Express.js is a minimal server-side web framework for Node.js. It helps build web applications with little overhead, using npm to install packages and package.json to track dependencies. section 2.2
A small Express application can:
- Create an application instance and listen on a chosen port, using PORT when supplied or 3000 by default. section 2.2.1
- Map an HTTP method and URL path to a callback with the form app.<httpMethod>(<pathString>, <callback>). section 2.2.1
- Return HTML responses or JSON data from routes. section 2.2.2 section 2.2.3
Hello Express World
Initialize a project with npm init, install Express using npm install express, and create main.js. The application can define app.get('/', (req, res) => res.send("<h1>Hello Express World!</h1>")) and start with app.listen(port, ...). Run it from the source directory with node main.js; set PORT=3011 to test another port. section 2.2 section 2.2.1 section 2.2.2
Listing 2.17 Express.js module in main.js
const express = require('express'); //#1
const fs = require('fs');
const path = require('path')
const app = express();
const port = process.env.PORT || 3000; //#2
app.get('/', (req, res) => {
res.send("<h1>Hello Express World!</h1>");
})
app.listen(port, () => {
const appPid = path.resolve(__dirname, 'app.pid')
fs.writeFileSync(appPid, `${process.pid}`);
console.log(`Server running on port http://127.0.0.1:${port}`);
})
Inspect the complete main.js example, including imports, application creation, PORT selection, the root route, and startup behavior. section 2.2.1
JSON route
Add a route such as /api/v2/rocket-man1/ that creates an object, converts it with JSON.stringify(), and returns it with res.json(). This gives the application both HTML and JSON response paths. section 2.2.3



