Script Valley
REST API Development: Beginner to Production
Building Your First API with Node.js and ExpressLesson 2.1

Setting up Express.js for REST API development

Node.js setup, Express installation, app initialization, listen method, port configuration, nodemon, package.json scripts

Setting Up an Express API Server

Express is the de-facto Node.js web framework for building APIs. It adds routing, middleware, and request handling on top of Node's bare http module without getting in your way.

Initial Setup

mkdir my-api && cd my-api
npm init -y
npm install express
npm install --save-dev nodemon

Add a dev script to package.json so the server restarts on file changes:

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

Minimal Express Server

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

app.use(express.json()); // parse JSON request bodies

app.get('/', (req, res) => {
  res.json({ status: 'API is running' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});

Run npm run dev and hit http://localhost:3000 in a browser or with curl. The express.json() middleware line is required — without it, req.body is undefined on POST requests. Always put it before your routes.

Up next

Express routing — defining GET POST PUT DELETE routes

Sign in to track progress