Setting up an Express API

Kenny Marks
The Startup
Published in
4 min readJul 12, 2020

--

In this article I will be covering the basics on how to initaite an Express API. If you are unfamiliar with an Express it is a “Fast, unopinionated, minimalist web framework for Node.js.” By the end of this article we will see an output in both our terminal and our browser window when we navigate to our localhost port.

Creating the Express Project

The first thing we are going to do is set up new directory to house our new API and then enter that directory. So in our terminal we are going to run the command :

$ mkdir example-api$ cd example-api

Next we will create a package JSON by running the following:

$ npm init -y

Lastly we will install express so that we can use it:

$ npm install express 

Setting up the API

Now that we have initalized our project of we open it in our editor we will see this:

The next step we will do is to create a root file for the project. To do so I am going to create an new folder called ‘src’ and then create a new javaScript file called ‘index.’ You can name this file something else for example app, the name of your project or whatever works for you:

Now that we have an index file set up we can start by creating our express aplication. Firts we will require the express library and save it to a variable.

const express = require('express')

Then we are going to create an app object by calling on express.

const app = express()

Next we are going to create our root route using the ‘app.get’ method. This will make a GET type HTTP request to our application. This funciton will get called with a request object and a response object. Here we are just going to send back a string response of “Hello from Express!” :

app.get('/', (req, res) => {
res.send("Hello from Express!")
})

Now whenever we navigate to the root (‘/’) of our API the browser window will read:

Hello from Express!

However before we can navigate to our API we must first set up a port for it to listen on. We can do this by using the ‘app.listen’ method. This method will take in two arguemnts the address of the port we want our app to listen on (in this example 3000, and then a callback function which we will use to run a console.log to let us know that the server has started.

app.listen(3000, () => {
console.log('Listening on port 3000')
})

Starting the API

To start our API we just need to run:

$ node src/index.js

When we hit enter we should see:

And finally if we navigate to our local host we should see:

And with that we have initalized our API!

Using Nodemon

We could stop here but if we add one more dependency we can make changes to our API and not have to worry about starting and stopping our server every time.

First we just have to install the library:

npm install nodemon

Now we just need add a script in our package.json file:

{
"name": "example-api"
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}

Now when we start our sever instead of running “node src/index.js” we can just run “npm run dev”

$ npm run dev

Our server will now update with every change.

Code Snippets

//Indexconst express = require('express')
const app = express()
app.get('/', (req, res)=> {
res.send('Hello from Express!')
}
app.listen(3000, () => {
console.log('Listening on port 3000')
})
// Package Json{
"name": "example-api"
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}

Conclusion

We have now initialized an Express API. Now we can begin building out a larger Node.js based API. Happy Coding!

--

--

Kenny Marks
The Startup

A full stack developer with an interest in Cybersecurity, International Relations, and Gaming.