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.

4330 bites

Page 71

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

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

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

Write a request-logging middleware in Express

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

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

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

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

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

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

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

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

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

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

Idempotency: PUT vs POST in REST

Idempotent means repeated identical requests leave the same server state; PUT is idempotent, POST is not. Use PUT to overwrite a resource at a known URL.

Node.js & Express1 min read

404 vs 500: missing resource vs server failure

A missing resource returns 404 Not Found (client asked for something absent); a database failure returns 500 Internal Server Error (server-side fault).

Node.js & Express1 min read

Centralized error handling in an Express API

A final four-arg error middleware, an asyncHandler wrapper to funnel promise rejections via next, a custom error class with statusCode, returning uniform JSON.

Node.js & Express1 min read

API versioning: URL vs header strategies

Version via URL path (/v1/), a custom or Accept header, or a query param; URL is visible and cache-friendly, headers keep URLs clean but are less discoverable.

Node.js & Express1 min read

Define a Mongoose schema and model

New mongoose.Schema with field options (type, required, default), then mongoose.model('Product', schema) to get a model.

Node.js & Express1 min read

Mongoose populate() for referenced documents

Populate() replaces stored ObjectIds with the referenced documents, needs a ref in the schema, called via .populate('author').

Node.js & Express1 min read

Database migrations with the Sequelize CLI

Migrations are version-controlled scripts with up/down so teams apply identical schema changes; use sequelize-cli to generate, edit with addColumn, then db:migrate.