Web Server Using Node.js
Hello! This is my first tutorial, so if you find any mistakes or anything you want me to fix please say so in the comments!
How To Make A Web Server Using
Step 1: Include modules
For this we need the 'express' module
we can do this by using these lines of code:
we will also need the 'path' module:
Step 2: Set up the Express router
when the user goes to https://www.[yourwebsite].com/
they will receive index.html.
but what if you want to have different pages?
Duplicate what we did before except, instead of just '/' you use '/yourpage'
or in my case, '/lol'
(you can see it in action here: https://web-server-template--codingandmemes.repl.co/lol )
Side note: Errors
To send the user to a page when an error occurs, it is very similar to the navigation(see a 404 error in action here: https://web-server-template--codingandmemes.repl.co/thisnoexist )
on a 404 Error:
For any other error, you just have to update the
res.status. for instance, on a 403 error:
res.status(403) and so on, you also along with that have to update the directory:
res.sendFile(__dirname + '/403.html');
Set up the Express server to listen on port 3000
And thats it! You can check out my template below!
(Note: if you want your server to stay up 24/7-ish use UptimeRobot. It pings your repl every 5 minutes, or the interval you set it too.)
Here is an example of my uptime robot for my server(its free)