Node.js (Express)

JavaScript on the server - one language for everything.

What is it?

Node.js is a runtime that lets JavaScript - the language of the browser - run on a server. That means the same language you use for frontend interactivity can also handle requests, talk to databases, and build APIs. Express is the most popular Node web framework: a thin, flexible layer that maps URLs to functions and handles the request/response details.

Together they power one of the largest developer ecosystems in the world. npm, Node's package registry, has a library for nearly anything you'll ever want to do.


When to use it

Node + Express is the natural backend if you're already learning JavaScript for the frontend - one language across the whole stack, and it pairs seamlessly with React or Vue. It's especially good for APIs and real-time apps. It's happiest on a VPS or platform service (only some shared plans can run Node apps, and on classic shared hosting PHP wins), and if you'd rather learn the most readable language first, Python + Flask is the gentler start.


Core concepts

  • npm and package.json - every project declares its dependencies in package.json; npm install fetches them into node_modules.
  • Routes - Express maps an HTTP method and URL to a handler function: app.get("/users", handler).
  • Middleware - functions that run on every request before your handler: parsing JSON, logging, checking logins. Express is essentially a middleware pipeline.
  • Async by design - Node never waits idle. Slow operations (database calls, file reads) use async/await so the server keeps handling other requests meanwhile. This is Node's superpower and the biggest beginner hurdle.
  • JSON APIs - most Node backends serve JSON to a JavaScript frontend rather than rendering HTML themselves.

A taste of the code

import express from "express";

const app = express();
app.use(express.json());

app.get("/api/hello", (req, res) => {
  res.json({ message: "Hello from Express!" });
});

app.listen(3000, () => console.log("Running on port 3000"));

Run node server.js and you have a JSON API at localhost:3000/api/hello - ready for a React or Vue frontend to fetch from.


Your first steps

  1. Be solid on JavaScript fundamentals - especially functions, objects, and promises/async.
  2. Install Node.js (the LTS version), then run npm init -y and npm install express in a new folder.
  3. Copy the server above, run it, and hit the endpoint in your browser.
  4. Build a small REST API - a todo API with GET, POST, and DELETE routes storing data in an array.
  5. Swap the array for a real database like SQLite or PostgreSQL, then connect a frontend to it.

Common beginner mistakes

  • Mixing require and import. Node supports CommonJS and ES modules. For new projects, add "type": "module" to package.json and use import throughout.
  • Forgetting await. Without it you get a pending Promise instead of your data - often showing up as [object Promise] or an empty response.
  • Copying Express 4 error handling into Express 5. Express 5 catches errors from async handlers itself; you no longer need try/catch around every route.
  • Hard-coding the port. Hosts tell your app which port to use through process.env.PORT.
  • Installing tools Node now includes. node --watch replaces nodemon, node --env-file=.env replaces dotenv for most uses, and node --test is a built-in test runner.

Getting it online

Node apps need a host that runs a long-lived process: platform services like Render, Railway or Fly.io, or a VPS with a process manager. Some shared hosting plans offer a "Node.js app" option too, but support varies. Our Node + Express + PostgreSQL guide walks through a full deployment.


← Back to Stack Picker All primers Compare languages