U
U
Urukhayy2017-11-14 12:59:23
Node.js
Urukhayy, 2017-11-14 12:59:23

What are the situations in which you can't do without app.param in Express?

In what situations can you not do without app.param? After all, actions can be done immediately in app.get.

app.param('userId', (req, res, next) => {
    // some actions
    next()
})

app.get('/users/:userId', (req, res) => {
    // some actions
    res.end()
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2017-11-14
@Lynn

There are no such situations, but sometimes it is more convenient with him.
For example, when you have several different routes with this parameter.

app.param('userId', (req, res, next) => {
  // тут какой-то длинный код
  next();
});

app.get('/users/:userId', (req, res) => { ... });
app.get('/users/:userId/info', (req, res) => { ... });
app.post('/users/:userId', (req, res) => { ... });

There was no need to write the same code in all routes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question