Express Basics and Routing

Site Admin · 11 Sep 2026 · 6 views

Express Basics and Routing

Express is the most popular web framework for Node.js. It layers routing, middleware, and helpers on top of the http module so building servers feels fast and clear.

Installing and booting

npm install express
import express from 'express';

const app = express();

app.get('/', (req, res) => {
  res.send('Hello from Express');
});

app.listen(3000, () => console.log('Server on port 3000'));

app is the application object, routes register handlers, and listen starts the server.

Route methods

Each HTTP verb has a matching method:

app.get('/users', handler);
app.post('/users', handler);
app.put('/users/:id', handler);
app.delete('/users/:id', handler);

Route parameters

Colon segments capture dynamic parts of the path:

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

req.params.id holds the value from the URL, like 42 in /users/42.

Query strings

Data after the question mark arrives in req.query:

// GET /search?q=cat
app.get('/search', (req, res) => {
  res.json({ query: req.query.q });
});

Sending responses

res.send returns text or HTML, and res.json returns JSON with the right content type. Express infers content types and status codes automatically, with res.status available for fine control.

Key Points

  • Express is a minimal framework over the http module.
  • app.get, post, put, and delete define routes.
  • :param in paths maps to req.params.
  • Query strings arrive in req.query.
  • res.json and res.send shape responses.
Share this post:

Comments (0)

Please login or register to comment.