Code Your First API With Node.js and Express: Set Up the Server

How to Set Up an Express API Server in Node.js

In the previous tutorial, we learned what the REST architecture is, the six guiding constraints of REST, how to understand HTTP request methods and their response codes, and the anatomy of a RESTful API endpoint.

In this tutorial, we’ll set up a server for our API to live on. You can build an API with any programming language and server software, but we will use Node.js, which is the back-end implementation of JavaScript, and Express, a popular, minimal framework for Node.

Installation

Our first prerequisite is making sure Node.js and npm are installed globally on the computer. We can test both using the -v flag, which will display the version. Open up your command prompt and type the following.

node -v && npm -v

v10.8.0
6.2.0

Your versions may be slightly different than mine, but as long as both are there, we can get started.

Let’s create a project directory called express-api and move to it.

mkdir express-api && cd express-api

Now that we’re in our new directory, we can initialize our project with the init command.

npm init

This command will prompt you to answer some questions about the project, which you can choose to fill out or not. Once the setup is complete, you’ll have a package.json file that looks like this:

In the introductory REST article, we discussed what requests and responses are with regards to an HTTP server. We’re going to set our server to handle a request and display the URL requested on the server side, and display a Hello, server! message to the client on the response side.

To check that the server is actually running, go to http://localhost:3001/ in your browser’s address bar. If all is working properly, you should see Hello, server! on the page. In your terminal, you’ll also see the URLs that were requested.

URL: /
URL: /favicon.ico

If you were to navigate to http://localhost:3001/hello, you would see URL: /hello.

We can also use cURL on our local server, which will show us the exact headers and body that are being returned.

Now, instead of looking for all requests, we will explicitly state that we are looking for a GET request on the root of the server (/). When / receives a request, we will display the URL requested and the “Hello, Server!” message.

Add Body Parsing Middleware

In order to easily deal with POST and PUT requests to our API, we will add body parsing middleware. This is where our body-parser module comes in. body-parser will extract the entire body of an incoming request and parse it into a JSON object that we can work with.

We’ll simply require the module at the top of our file. Add the following require statement to the top of your app.js file.

const bodyParser = require('body-parser');
...

Then we’ll tell our Express app to use body-parser, and look for JSON.

If you send a curl -i to the server, you’ll see that the header now returns Content-Type: application/json; charset=utf-8.

Set Up Routes

So far, we only have a GET route to the root (/), but our API should be able to handle all four major HTTP request methods on multiple URLs. We’re going to set up a router and make some fake data to display.

Let’s create a new directory called routes, and a file within called routes.js. We’ll link to it at the top of app.js.

const routes = require('./routes/routes');

Note that the .js extension is not necessary in the require. Now we’ll move our app’s GET listener to routes.js. Enter the following code in routes.js.