Don't store request specific data into the Layer object by Congelli501 · Pull Request #113 · pillarjs/router

Issue

Currently, the path & params of the latest request is stored into the last matched layer.
The "Layer" object should be immutable: a request should not modify its attributes.

This means that parameters are kept into memory after the end a request.

Fix

This PR simply removes the path & params parameters of the Layer object, and return them as the result of the match function.

Test case

A test case is included, to check that the params attribute is not available anymore in the Layer.

History

An initial PR was made on the express project: expressjs/express#5426

Example

For example, this code will show the latest value of the parameter, as the value is stored until a new request is made:

const express = require('./index.js');
const app = express();
const port = 3000;

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

app.listen(port, () => {
	console.log(`Example app listening on port ${port}`);
});

setInterval(() => {
	console.log(app._router.stack.at(2).params);
}, 1000);
curl localhost:3000/secret/super_secret

Result:

Example app listening on port 3000
undefined
undefined
undefined
{ token: 'super_secret' }
{ token: 'super_secret' }