Skip to content
← Back to Community
Web Server Using Node.js
Profile icon
thedankboi

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 Node.js


Step 1: Include modules

For this we need the 'express' module
we can do this by using these lines of code:

const express = require('express');//Set up the express module const app = express(); const router = express.Router();

we will also need the 'path' module:

const path = require('path')

Step 2: Set up the Express router

router.get('/', function(req, res){ res.sendFile(path.join(__dirname, '/index.html/index.html')); }); app.use('/', 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'

//if they go to '/lol' router.get('/lol', function(req, res){ res.sendFile(path.join(__dirname, '/lol.html')); }); app.use('/lol', router);

(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:

//404 Error app.use(function(req, res, next) { res.status(404); res.sendFile(__dirname + '/404.html'); });

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

let server = app.listen(3000, function(){ console.log("App server is running on port 3000"); console.log("to end press Ctrl + C"); });

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)

Uptime Robot Screen Capture

Voters
Profile icon
haamidahalonge
Profile icon
thedankboi
Profile icon
resourcecentre
Profile icon
Grimicron
Profile icon
MattDESTROYER
Profile icon
LeOtFs2
Profile icon
zuw
Profile icon
PedroGXV
Profile icon
CATSAG7PRO
Profile icon
Yoplayer1py
Comments
hotnewtop
Profile icon
Raphtalia

tutorial seems fine but isn’t there already a lot of express tutorials on repl talk already?

Profile icon
ch1ck3n

thanks for telling me about uptimebot+

Profile icon
Yoplayer1py

@ch1ck3n I used uptime robot in my discord bot. It's useful.

Profile icon
Vandesm14

You don't need to use express.Router(), you can just use app: app.get and so on

And also, .htaccess doesn't work with Express, so you don't need that either.

Profile icon
thedankboi

I know that .htaccess doesn’t work with Express, see the “Side note: Errors” section @Vandesm14

Profile icon
Vandesm14

@CodingAndMemes Oh, my bad!

Profile icon
thedankboi

Its fine, @Vandesm14 , I did say that criticism was welcome at the top

Profile icon
codingclub1934

Is it possible to use a custom domain on a node js server it would be great if it is

Profile icon
thedankboi

@codingclub1934 It is! Just click the pencil icon next to where your repl.co link is.

Profile icon
codingclub1934

@CodingAndMemes can you dm me on discord if you have BeastCodZ#1470 i dont know how to get custom domain in node js template pls help

Profile icon
NotKarmaDev

how do i find my link

Profile icon
MrCSoldier

as simple as typing 'npm init'. Good Tutorial! Ill look back into it if I ever Need to do the same thing again

Profile icon
aguy11

Hi! This tutorial is really nice. But how do you apply external CSS?

Profile icon
thedankboi

@aguy11 just like a regular HTML file or project on repl.it!

Profile icon
aguy11

@CodingAndMemes I'll try it again but I'm pretty sure it doesn't work. EDIT: It doesn't work.

Profile icon
ch1ck3n

@CodingAndMemes make a another nodejs line that puts the css on the server, then link the css file to html.

Profile icon
pluto7073

@ch1ck3n How would I go about doing that? I tried res.sendFile but it didn't work

Profile icon
thedankboi

@aguy11 see the folder called index.html