← Back to all tutorials

Setting up Node.js

Install Node.js, initialize a project with npm, and understand the Node.js runtime that will power your REST API server.

Setting up Node.js

To build a REST API, you need a server-side runtime that can handle HTTP requests. Node.js is a JavaScript runtime built on Chrome's V8 engine that lets you run JavaScript outside the browser — perfect for building servers and APIs.

What Is Node.js?

FeatureDescription
RuntimeRuns JavaScript outside the browser (on your computer/server)
V8 EngineUses the same fast engine that powers Google Chrome
Non-blockingHandles many requests simultaneously without waiting for each one to finish
npmComes with the world's largest package manager (npm)
Single-threadedUses one thread with an event loop (efficient for I/O-heavy work like APIs)

Step 1: Install Node.js

Download and install Node.js from nodejs.org. The LTS (Long Term Support) version is recommended for stability. The installer includes both node and npm.

node --version
// v20.x.x

npm --version
// 10.x.x

Step 2: Create the Project

mkdir rest-api
cd rest-api
npm init -y

The npm init -y command creates a package.json file with default settings. This file tracks your project's metadata, scripts, and dependencies.

Understanding package.json

{
    "name": "rest-api",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "dev": "node index.js"
    },
    "keywords": [],
    "author": "",
    "license": "ISC"
}
FieldPurpose
nameProject name (used if published to npm)
mainEntry point file for the project
scriptsCommands you run with npm run scriptName
dependenciesLibraries your project needs to run
devDependenciesTools needed only during development

Step 3: Create the Entry File

Create index.js in the project root:

// A simple Node.js HTTP server (no Express yet)
const http = require('http');

const server = http.createServer((req, res) => {
    res.setHeader('Content-Type', 'application/json');
    res.end(JSON.stringify({
        message: 'Hello from the REST API!',
        method: req.method,
        url: req.url,
    }));
});

const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Server running on http://localhost:${PORT}`);
});

Step 4: Run the Server

node index.js

Open http://localhost:3000 in your browser and you will see a JSON response. This is a basic HTTP server using only Node.js built-in modules — no libraries needed.

The Built-in http Module

const http = require('http');

Node.js comes with a built-in http module for creating servers. The createServer method takes a callback that runs for every incoming request. The callback receives a request object (data about the incoming request) and a response object (used to send data back).

Why We Need Express

The built-in http module works but is very low-level. For a real API, you would need to manually parse URLs, handle different HTTP methods, parse request bodies, manage routing, and more. Express handles all of this with a clean, simple API:

FeatureRaw http ModuleExpress
RoutingManual if/else on req.urlapp.get('/route')
Body parsingManual stream readingexpress.json() middleware
Static filesManual file readingexpress.static()
MiddlewareNot built-inapp.use()
Error handlingManual try/catchBuilt-in error middleware

Installing nodemon (Auto-restart)

npm install nodemon --save-dev

nodemon automatically restarts your server when files change — no need to manually stop and start after every edit.

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

Now the server restarts automatically when you save any file. Use npm run dev during development and npm start for production.

Project Structure

rest-api/
├── index.js         (server entry point)
├── package.json
├── package-lock.json
└── node_modules/

Key Takeaways

  • Node.js lets you run JavaScript on the server — it is the foundation for building REST APIs
  • npm init -y creates a project with a package.json to track dependencies and scripts
  • Node's built-in http module can create servers, but it is low-level and verbose
  • Express (next episode) simplifies routing, body parsing, and middleware
  • nodemon auto-restarts the server on file changes — essential for development