WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Intermediate · question 32 of 100

What are some common middleware patterns in Express.js applications?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

Express.js middleware are functions that have access to the request object (‘req‘), response object (‘res‘), and the next function in the application request-response cycle. The next function is a function in the Express.js router that, when invoked, passes control to the next middleware or route handler in the cycle.

Here are some common middleware patterns in Express.js applications:

1. **Application-level middleware:** Middleware that is bound to the express application using ‘app.use()‘. This middleware runs for all requests on the app.

Example:

const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

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

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

2. **Router-level middleware:** Middleware that is bound to an instance of ‘express.Router()‘. This middleware runs for all requests on the router.

Example:

const express = require('express');
const router = express.Router();
const app = express();

router.use((req, res, next) => {
  console.log('Time:', Date.now());
  next();
});

router.get('/', (req, res) => {
  res.send('Hello World!');
});

app.use('/', router);

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

3. **Error-handling middleware:** Middleware that specifically handles errors. Error-handling middleware is defined with four arguments, in the following format: ‘(err, req, res, next)‘.

Example:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  throw new Error('An error occurred!');
});

app.use((err, req, res, next) => {
  console.error('Error:', err.message);
  res.status(500).send('Something went wrong :(');
});

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

4. **Built-in middleware:** Middleware that is available directly in the Express.js framework, for example, ‘express.static()‘ or ‘express.json()‘.

Example:

const express = require('express');
const app = express();

app.use(express.json());
app.use(express.static('public'));

app.post('/api/data', (req, res) => {
  console.log(req.body); // JSON data is automatically parsed by express.json()
  res.send('Data received');
});

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

5. **Third-party middleware:** Middleware available as third-party packages that can be installed and used in an Express.js application, for example, ‘cors‘ or ‘body-parser‘.

Example:

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/', (req, res) => {
  res.send('Hello World with CORS enabled!');
});

app.listen(3000, () => {
  console.log('Example app listening on port 3000!');
});

These are some of the common middleware patterns in Express.js applications. Depending on your application’s needs, you can mix and match these patterns to build the necessary functionality in your application.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics