Script Valley
REST API Development: Complete Course from Beginner to Production
Building REST APIs with Express.jsLesson 3.1

Introduction to Express.js: Setup and First Endpoint

Express.js introduction, npm init, express install, app.listen, routing basics, req res, first REST endpoint, nodemon

Introduction to Express.js: Setup and First Endpoint

Express.js is the most widely-used Node.js web framework for building REST APIs. It is minimal, flexible, and has a massive ecosystem of middleware. In this lesson, you will install Express, create your first server, and write your first REST API endpoint.

DiagramExpress.js Application Architecture

IMAGE PROMPT (replace this block with your generated image):

Flat folder-tree + flow diagram on white background. Title: Express.js Project Setup. Left side: a folder tree structure showing: my-api/ folder containing src/ (with index.js, app.js, routes/ folder, controllers/ folder, models/ folder), package.json, .env, .gitignore, node_modules/ (grayed out). Each file/folder has a small icon. Right side: a vertical flow showing how a request moves through the app — Step 1: node src/index.js starts server (box in #3A5EFF). Step 2: app.use(express.json()) parses body. Step 3: app.use('/api/users', userRouter) matches route. Step 4: Controller function runs. Step 5: res.json() sends response. Steps connected by down arrows in #3A5EFF. Clean monospace for code snippets, sans-serif for labels. White background.

Project Setup

Create a new project directory and initialize a Node.js project:

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

Add a start script to package.json:

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

Creating the Express Server

Create src/index.js:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.use(express.json());

app.get('/health', (req, res) => {
  res.status(200).json({
    success: true,
    message: 'API is running',
    timestamp: new Date().toISOString()
  });
});

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

express.json() is middleware that parses incoming request bodies with Content-Type: application/json. Without it, req.body will be undefined.

Route Parameters and Query Parameters

app.get('/users/:id', (req, res) => {
  const { id } = req.params;
  const { include } = req.query;
  res.json({ id, include });
});

req.params contains path parameters. req.query contains query string parameters. req.body contains the parsed JSON body. req.headers contains all request headers.

Organizing Routes with Express Router

As your API grows, keep all routes in separate files using Express Router:

const router = express.Router();

router.get('/', getAllUsers);
router.get('/:id', getUserById);
router.post('/', createUser);
router.put('/:id', updateUser);
router.delete('/:id', deleteUser);

module.exports = router;

Then mount the router in your main file: app.use('/api/users', userRouter);

Up next

Connecting to a Database and Performing CRUD Operations

Sign in to track progress