0

I am trying to implement some middleware in Express that should be called for all routes. This middleware should alter the request object.

I've tried several things already but seem to keep having the same issue. Soon as the middleware is left it looks like the request object is changed back to it's original state.

Currently my code resembles (I simplified it with a minimalistic example):

route.js:

const express = require('express');
const router = express.Router();
router.get('/getMe', (req, res) => {
  // return the desired data.
  // I expect req.params.myString to exist here but it does not.
});
module.exports = router;

index.js:

const express = require('express');
const router = express.Router();
router.use('/', require('./route'));
module.exports = router;

app.js:

const express = require('express');
const app = express();
const routes = require('./index');

app.use((req, res, next) => {
  // Adding req.params.myString to the request object.
  if (req.params.myString === undefined) req.params.myString = 'hello world';
  next();
});

app.use('/api', routes);

As you can see I left out some of the code to keep it more readable. This is the code that gets the response and sets up the server.

Again, I am expecting that req.params.myString becomes available in the endpoint. Does anyone see what I am doing wrong?

SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42

2 Answers2

2

In express docs ( http://expressjs.com/en/api.html#req.params ) it says:

If you need to make changes to a key in req.params, use the app.param handler. Changes are applicable only to parameters already defined in the route path.

So you need to check app.param handler.

http://expressjs.com/en/4x/api.html#app.param

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54
  • Could you please provide me with a working example. For some reason I am still not able to get this to work. I found an other question that said you cannot apply anything to the req object but you can to res. So now I am trying to do that but it seems like req.params.myString does not exists even if I use a route that declares it. – SomeDutchGuy Oct 22 '19 at 12:00
  • Hi, I actually didn't try this, but as stated in the docs, it seems possible if the requirements are ok. Did you have a look at the example here http://expressjs.com/en/4x/api.html#app.param – SuleymanSah Oct 22 '19 at 12:02
1

You should app.set("myString", "hello World") inside your app.js and then you can access the field in your route.js/index.js scripts by using req.app.get("myString"). Or this should work too, set it like app.myString = "Hello world" and access it like req.app.myString.

Untimely Answers
  • 512
  • 1
  • 6
  • 18
  • I believe this does not work at app level for the params object that is nested inside the request as it is reset when it is passed trough from what I understand. I will try it a bit later but I also found an other solution by applying the middleware directly to each route. In there the req.params object is accessible. – SomeDutchGuy Oct 23 '19 at 07:48