The 2021 Node.js + Express Beginner’s Guide

Bryan Dijkhuizen
Nov 30, 2020 · 3 min read
Image for post
Image for post
By Caspar Camille Rubin on Unsplash

Node.js is an open-source server environment, and Node.js allows you to run JavaScript on the server. By installing Express, running a web server never was easier, so let’s get into this practical guide about Express.

Getting Started

Once you have installed NodeJs, you can initialize your first Node.js project:

bash > npm init -y

When you have run the init command you can add a start script in the ‘package.json’:

"scripts": {
"start": "node index.js"
},

After that, you need to install the express framework:

bash > npm install express

When everything has been installed, we can go to our index.js file and add the following lines:

const express = require('express')
const app = express()

We are creating two constants variables called ‘express,’ which imports the express framework and ‘app’ that initializes it.

Running a web server.

To run a basic web server, we need to define a port to run on. Pick a free port, 3000 is recommended and common. Add the following code:

app.listen(3000)

The full code might look like this now:

const express = require('express')
const app = express()
app.listen(3000)

When you run it, nothing happens or at least seems to, but in the background, a web server runs on port 3000. If you want a load message, you can modify the following line:

app.listen(3000,
() => console.log('Server is running on port 3000')
);

Routes

To add routes to your web server, you need to add the following lines:

app.get('/', function (req, res) {
res.send('hello world')
})

‘App’ refers to the express framework, ‘get’ is the type of the request, and after that, a callback function is added with two arguments: ‘req’ and ‘res’ (request and response). So within the function, we send a ‘res’ (response) with the text of Hello World.

Full code will look like this:

const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('hello world')
})
app.listen(3000)

Router

A router object is an isolated instance of middleware and routes. You can think of it as a “mini-application,” capable only of performing middleware and routing functions. Every Express application has a built-in app router.

— Organizing with express.Router() — DEV. https://dev.to/jameseaster/organizing-with-express-router-3i82

An example:

router.get('/route', function (req, res, next) {
// route code
})

If you want to use the routes of the router you need to export this from an external file like routes.js:

const express = require('express');
const router = express.Router();
Router.get('/route', function (req, res, next) {
// route code
});
module.exports = router;

And you need to import it into your index.js file like this:

const express = require('express')
const app = express()
const routes = require('./routes.js');
app.get('/', function (req, res) {
res.send('hello world')
});
app.use('/routes', routes);app.listen(3000);

Conclusion

We are done with this article in understanding some basic concepts and I hope you are able to create simple web servers and application with Express now.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Bryan Dijkhuizen

Written by

Pursuing my Bachelor in Computer Science. Dutch parliament ambitions. Editor of Lessons From History — Evolve You — Political Sense and History in Context.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Bryan Dijkhuizen

Written by

Pursuing my Bachelor in Computer Science. Dutch parliament ambitions. Editor of Lessons From History — Evolve You — Political Sense and History in Context.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store