An ExpressJS Quickstart

In this post we're going to cover how to create your own local web server using the express package. It's a pretty straightforward process, so let's begin!

This is a crosspost of an article I wrote over on the Yordevs website a few weeks ago.

Setting up our project

If you haven't already, set up a new Node project. From there we just need to install express with the following command:

npm install express

This will download everything that we need to get a basic web server up and running!

The code

If you're just looking for some code to copy and paste then the following snippet will be enough.

// index.js
const express = require("express");
const app = express();

app.get("/", (request, response) => {
	res.send("Hello, world!");


To run the server just type:

node index.js

You can test it working by visiting localhost:3000 in your web browser, and should see something like this:

A Deeper Explanation

If you fancy going into a little more detail, in this section we'll split up the code above into a few parts and explain what they each do. As a quick reminder, here's the snippet again (but with comments too!):

// 1. Import and setup
const express = require("express");
const app = express();

// 2. Setting up endpoints
app.get("/", (request, response) => {
	response.send("Hello, world!");

// 3. Listening for requests

1. Import and setup

In this section we're doing two things. First, we're importing our newly installed express package with:

const express = require("express");

Next, we create an instance of the Express server by typing:

const app = express();

Creating instances is helpful in letting us create multiple servers with different "names" that can be referenced for different things. For example you could create three different servers and have them set up to do different things, all without interfering:

const server1 = express();
const server2 = express();
const server3 = express();

// Each server can be set up to listen on a different port (explained later on) and won't interfere with each other.

2. Setting up endpoints

A server's endpoints are basically URLs that can be used to access different kinds of data. In the example above with have an endpoint like so:

app.get("/", (request, response) => {
	response.send("Hello, world!");

This creates an endpoint for our app server which responds to GET requests sent to the URL localhost:3000.

If you wanted to add another endpoint which did something when you sent a request to localhost:3000/ping, it would look something like:

app.get("/ping", (request, response) => {

Try this part out in your code, if you want! Consider it a small challenge or something.

The final part is a callback which takes the request and response as parameters, allowing you to process the request to the server (e.g. parsing parameters included in the URL) and your response before you send it back.

3. Listening for requests

So far we've got a server which is based on the ExpressJS package we installed, and a few endpoints which are set up to do some stuff, but how do we actually get the server responding to our requests?

Much like a real life conversation, we need the server to be listening for any requests that are sent to it.

This is, thankfully, a really easy thing to do.


Here we're literally telling the server, "listen out for any requests sent to port 3000" .

For reference, a port is like a way of dividing a network connection into lots of little numbered "doors" which can then have different types of network traffic assigned to them (e.g. all requests going to our server are on port 3000, all HTTP requests use port 80).

But yeah, that's what listening for requests is!

With all that said and done, you should have your very own web server up and running on your computer. Congratulations!