Skip to content

Install Node.js and run your first Express server

my first blog post

JavaScript’s global scope is like a public toilet. You can’t avoid going in there, but try to limit your contact with surfaces when you do. — Dmitry Baranovskiy

This post installs Node.js, then builds a small Express server: one route, an npm start script, and a folder of static files.

Pick the LTS (long-term support) release unless you have a reason not to.

  • Download the LTS installer (.msi) from nodejs.org and run it, or
  • use Chocolatey: choco install nodejs-lts.

You can also run Node inside the Windows Subsystem for Linux. On current Windows 10 and 11, wsl --install in an administrator terminal installs WSL with Ubuntu; then follow the Linux instructions below inside it.

Terminal window
brew install node
Terminal window
sudo apt update
sudo apt install nodejs npm

The Ubuntu repository version usually lags well behind the current LTS. If you need a newer release, use a version manager such as nvm or the NodeSource packages linked from the Node.js download page.

Terminal window
node -v
npm -v

Express 5 needs Node.js 18 or newer, so check the version printed here.

Express is a small web framework for Node.js. Its core is routing (map an HTTP method and path to a handler) and middleware (functions that run on each request before your handler). Things like sessions, authentication, and file uploads come from separate middleware packages.

Create a project and install Express:

Terminal window
mkdir my-node-app
cd my-node-app
npm init -y
npm install express

Save this as app.js:

const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello, world!");
});
app.listen(3000, () => {
console.log("Server listening on port 3000");
});

Run it with node app.js and open http://localhost:3000. A GET / request returns “Hello, world!”; any other path gets Express’s default 404 response.

Add a scripts entry to package.json so you can start the app with npm start:

{
"name": "my-node-app",
"version": "1.0.0",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "^5.3.0"
}
}

npm init and npm install will already have written most of this file; you only need to add the start line. Then:

Terminal window
npm start

start is one of the few scripts npm runs without run; for any other script name use npm run <name>.

express.static is built-in middleware that serves files from a directory:

const path = require("path");
const express = require("express");
const app = express();
app.use(express.static(path.join(__dirname, "public")));
app.listen(3000, () => {
console.log("Server listening on port 3000");
});

A file at public/index.html is then available at http://localhost:3000/index.html (and at /, because express.static serves index.html for a directory by default). The directory name is not part of the URL.

Use path.join(__dirname, ...) rather than a bare 'public'. A relative path is resolved against the directory you launched node from, so node src/app.js run from the project root would look in the wrong place.

express.static only serves files. Anything dynamic, such as form submissions or API calls, needs its own route handlers registered with app.get, app.post, and so on.

This content came from a book I generated using ChatGPT and a lot of editing.

View the source material on Amazon: https://www.amazon.com/dp/B0BSL14M71

For a free copy, please view https://friendlyuser.github.io/assets/pdfs/introToJs.pdf

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)