How to Create a Node Express API

Make sure node is installed

Make sure npm works by running npm -v, you should see a version number

Create new directory called express-example

Run cd express-example

Run git init

Run npm init -y

Run npm install express

Create .gitignore with /node_modules/

Create app.js with the following content:

const express = require('express')
const app = express()
const port = process.env.PORT || 5000

app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

Run node app.js and navigate to localhost:5000 to see server response

Add start script to package.json: "start": "node app.js"

Commit code with git commit

Create heroku account

Install heroku CLI

Run heroku create

Run git push heroku master

Load the URL of the new app and confirm it is working

Congratulations! You have your first Node Express API up and running!

Leave a comment

Design a site like this with WordPress.com
Get started