Skip to content
tezvyn:

All bites

The whole library, newest first. Filter by what you are here for, or pick a topic if you already know.

8664 bites

Page 19

Node.js & Express1 min read

Modularize routes with express.Router

Create a Router per resource in its own file, define routes on it, export it, and mount with app.use('/products', router).

Node.js & Express1 min read

Status codes for successful POST and GET

201 Created for a successful POST (ideally with a Location header), 200 OK for a successful GET returning data.

Node.js & Express1 min read

req.params vs req.query vs req.body in Express

Req.params holds named route segments, req.query holds the URL query string, req.body holds the parsed payload.

Node.js & Express1 min read

Design an Express route to create a user

POST to a collection URL like /users, read the payload from req.body via express.json(), return 201 with the created resource.

Node.js & Express1 min read

Conditionally apply middleware by request property

Use express.text({ type: 'application/xml' }) or a guard wrapper that checks req.is() then calls the parser or next().

Node.js & Express1 min read

Write a JWT authentication middleware

Read the header, strip Bearer, jwt.verify with the secret, set req.user and next(), else send 401.

Node.js & Express1 min read

Middleware execution order and sharing data via req

Middleware runs top-down in registration order; each calls next(); attach data like req.user that later handlers read.

Node.js & Express1 min read

Express error-handling middleware signature

(err, req, res, next) with err first, identified by arity, placed last after all routes.

Node.js & Express1 min read

Application-level vs router-level middleware

App.use() binds to the app and runs everywhere; router.use() binds to a Router and runs only for that router's routes.

Node.js & Express1 min read

Write a request-logging middleware in Express

Read req.method, log with a timestamp, then call next() to continue.

Node.js & Express1 min read

Parse JSON and URL-encoded bodies in Express

Express.json() for JSON, express.urlencoded() for form data, both registered via app.use().

Node.js & Express1 min read

What is Express middleware

Middleware are functions in a chain with req, res, next, they inspect or modify request and response, then call next to continue or send a response to end.

Node.js & Express1 min read

Centralized error-handling middleware

Error middleware takes four args err, req, res, next, is defined last, and runs when next(err) is called or sync errors throw.

Node.js & Express1 min read

Custom API key auth middleware

Read the header from req, on missing or invalid send res.status(401) and return, on valid call next, mount before protected routes.

Node.js & Express1 min read

Modularizing routes with express.Router

Create a Router instance in users.js, attach routes to it, export it, then mount it under a base path with app.use in the main file.

Node.js & Express1 min read

Express route order and matching

Express checks routes in definition order, /items/new matches the literal route first, if /:id came first new would be captured as an id.

Node.js & Express1 min read

res.send vs res.json vs res.end

Send is flexible and sets content type by type, json serializes and sets JSON content type, end is the raw http terminator with no body helpers.

Node.js & Express1 min read

Parsing JSON bodies with express.json

Register express.json via app.use so it reads the request stream and populates req.body before handlers run, place it before routes.

Node.js & Express1 min read

Query params vs route params in Express

Query string values via req.query.q, named path segments via req.params.id, query is optional, route params are part of the matched pattern.

Node.js & Express1 min read

Serving static files in Express

App.use with express.static pointing at the public directory, files served relative to that root, often combined with an absolute path.