How to prepend a specific word in my server url in node
Hello guys, sorry to disturb you all; I have created a simple server using node js. The url of my server is always something like that:
localhost:xxxx/index.html
I have some questions first:
Why the full path of our file isn't displayed in the url but like if our index.html is in the folder views, we don't see that folder.
Next, what I wanted to do is, before every webpage, whether it is localhost:xxxx/about.html or localhost.xxxx/contact-me.html, I want to prepend a word, like localhost.xxxx/SOMETHING/index.html
11 Replies
your server uses something that's usually called the document root.
/
on the HTTP server refers to say /var/www/html/
or C:\Dev\mycoolproject\public\
so if you want to prepend something ahead of that, and you're just serving HTML files, put the HTML files in a subfolder of wherever your server is serving files from
say you have index.html here: /var/www/html/index.html
and it works, then you can put it here: /var/www/html/SOMETHING/index.html
and then go to http://localhost:xxx/SOMETHING/index.html
, it should still work
at its base, a web server just serves files from disk (unless you use a router, but even then it's serving the router and the router is doing the work of actually giving you the contents it wants), so the file structure on disk determines how the files are servedhmm the thing is I have a folder called "views" which serves the html pages but in my url I only find the index.html name, have a look
ah
2sec
so the views folder is acting like your document root
yeah, like I should have another folder in that folder
and that folder should contain everything else
anything you want accessible on the site, yes
yepp I see, ty !!
I just search it on the net, I noticed they used express to do some routing logic, I don't understand it yet, I will learn express but what express will do that we can't do right now ?
just serving static files only works if the data you want to serve is static. There's usually some need to show dynamic things. You don't want to have to store all 50k user profiles on your server as html and accessible for anyone that can guess that user13297.html belongs to someone they want info on. You generally use routers so that you can have programmatic control over who sees what, and to show dynamic content
it's definitely an It Depends / It's Complicated though, because some languages other than nodejs use file based routing and handle access and dynamic content differently
PHP for example just runs whatever .php file you point it at, and that file is responsible for checking if it should be accessed, and loading any dynamic content it should load. But that's way deeper than you need to go at this stage
yep... I will just start learning express and will revert back to clear my doubts
yeah, just focus on express for now, it does things its own way and it's complicated enough on its own
there's a lot of "ashkually ☝️ 🤓 " in there, but yes
web servers are complicated beasts, but the basics are simple and get extremely complicated when you use something that wasn't done specifically for websites
which is probably why node and express are so complex at it
some of the basic concepts can be passed on between languages, but there's a lot of magic behind