We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi,
I recently tried to upgrade to v5 from v4 and used the following guide:
https://expressjs.com/en/guide/migrating-5.html#path-syntax
Here is my app.all() that sets headers on all routes:
app.all()
app.all('/api/*', function (req, res, next) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader( 'Access-Control-Allow-Headers', 'Origin, Accepts, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Response-Time, X-CSRF-Token, Authorization', ); res.setHeader('Access-Control-Allow-Methods', '*'); res.setHeader('Access-Control-Expose-Headers', 'X-Api-Version, X-Request-Id, X-Response-Time'); res.setHeader('Access-Control-Max-Age', '1000'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Expires', '-1'); res.setHeader('Pragma', 'no-cache'); next(); });
As per documentation:
The wildcard * must have a name, matching the behavior of parameters :, use / *splat instead of / *
I changed it to:
app.all('/api/*splat'
This does not work. Anything I am doing wrong?
The text was updated successfully, but these errors were encountered:
It definitely should work. What exactly doesn't work in your case? Is the handler skipped or do you get an error?
This simplified code works as expected:
const express = require("express"); const app = express(); app.all("/api/*sth", (req, res, next) => { res.setHeader("my-header", "a value"); next() }); app.get("/*p", (req, res) => res.send("ok")); const server = app.listen(1234, () => fetch("http://localhost:1234/api/random/path").then(r => { console.log(r.headers); server.close(); }) );
Sorry, something went wrong.
In this case it may be easier to use app.use:
app.use
app.use("/api", (req, res, next) => { // set headers next(); });
.use matches on prefix (if path is specified), so there is no need to use the * parameter.
.use
*
No branches or pull requests
Hi,
I recently tried to upgrade to v5 from v4 and used the following guide:
https://expressjs.com/en/guide/migrating-5.html#path-syntax
Here is my
app.all()
that sets headers on all routes:As per documentation:
I changed it to:
This does not work. Anything I am doing wrong?
The text was updated successfully, but these errors were encountered: