I have this api request:
http://localhost:5000/api/courses/get_public_course_data_by_id?course_id=454545
And I have these two ExpressJs routes:
router.get("/:id", (req, res) => {});
router.get("/get_public_course_data_by_id", (req, res) => {});
For some reason, it's always the first endpoint that gets triggered and not the second.
You need to add static route before the dynamic route
Like This:
router.get("/get_public_course_data_by_id", (req, res) => {}); // 1st this
router.get("/:id", (req, res) => {}); // then this
The reason is, node router assuming get_public_course_data_by_id <-- this as id and processing the request accordingly and get_public_course_data_by_id is never executed.
Related
I have the following code with 3 independent routers
const Express = require("express")
const app = Express()
const usersRouter = Express.Router()
const productsRouter = Express.Router()
const storeRouter = Express.Router()
productsRouter.use((_, res, next) => {
res.send("products fail")
//next()
})
storeRouter.use((_, res, next) => {
res.send("store fail")
//next()
})
usersRouter.route("/users")
.get((_, res) => res.send("users"))
productsRouter.route("/products")
.get((_, res) => res.send("products"))
storeRouter.route("/store")
.get((_, res) => res.send("store"))
app.use(usersRouter)
app.use(productsRouter)
app.use(storeRouter)
app.listen(80, () => console.log("running"))
But every time I request /store route it pass through productRouter middleware which is assigned only to it.
I can't understand this behavior.
Why is this?
How can I manage independent middlewares for each one?
GET /store 200
products fail
Expected
GET /store 200
store fail
When you do this:
app.use(productsRouter)
that sends ALL requests to the productsRouter and thus its middleware runs for all requests. So, when you have this:
productsRouter.use((_, res, next) => {
res.send("products fail")
//next()
});
That will run on every single request.
If you want the router to only see certain requests, then register the router on a path instead so the router only gets requests destined for a certain path.
app.use("/products", productsRouter)
And, then remove the path itself from the router's routes since the path will have already been filtered.
In order to achieve the expected behavior, you will have to make little changes to your code.
First:
Take this approach, since it will allow you to keep everything clean and separated (this is crucial if you want to implement specific middlewares for each route).
usersRouter.
.get("/", (req, res) => res.send("users"))
productsRouter.route
.get("/", (req, res) => res.send("products"))
storeRouter.route("/store")
.get("/", (req, res) => res.send("store"))
app.use("/users", usersRouter)
app.use("/products", productsRouter)
app.use("/store", storeRouter)
Instead of this one
usersRouter.route("/users")
.get((_, res) => res.send("users"))
productsRouter.route("/products")
.get((_, res) => res.send("products"))
storeRouter.route("/store")
.get((_, res) => res.send("store"))
app.use(usersRouter)
app.use(productsRouter)
app.use(storeRouter)
Second:
Uncomment the next() call on your middlewares, identify the request parameter on their callbacks and store them in variables (not crucial, but improves readability)
const productsMiddleware = (req, res, next) => {
res.send("products fail")
next()
}
const storeMiddleware = (res, res, next) => {
res.send("store fail")
next()
}
Third:
Pass the middleware you want to apply to a specific controller right after the route and before the actual controller declaration on your router. E.G.
usersRouter.
.get("/", (req, res) => res.send("users"))
productsRouter.route
.get("/", productsMiddleware, (req, res) => res.send("products"))
storeRouter.route("/store")
.get("/", storeMiddleware, (req, res) => res.send("store"))
By doing all this things, you'll end up with "independent middlewares" that only apply to the specified route/controller.
Hello I need a second pair of eyes because I'm not sure why this is happening...
I want to create 1 request handler which might accept 0 or 1 or 2 parameters eg: http://hocalhost:3000/{seed}/{size}, seed and size parameters must be optional.
However the below example doesn't work and the console.log is being printed twice. This doesn't happen if I change the handlers route to /api/:seed?/:size?. Why is this happening & what am I doing wrong?
const sharp = require('sharp');
const express = require('express');
const settings = require('./settings');
const app = express();
const calculateDensity = (s) => {
return (72 * s) / 180;
}
app.get('/:seed?/:size?', (req, res) => {
console.log('Why am I seeing this log?');
res.end();
})
app.listen(settings.PORT, () => {
console.log(`Example app listening at http://localhost:${settings.PORT}`)
})
Browser automatically called favicon.ico after load.
it load twice
for favicon.ico and the route we define.
We can resolve it like below code
app.get('/favicon.ico', (req, res) => {
res.end()
})
app.get('/:seed?/:size', (req, res) => {
console.log(req.url)
if (req.url !== "/favicon.ico") {
console.log('Why am I seeing this log?');
}
res.end();
})
So this is your default route
app.get('/:seed?/:size?', (req, res) => {
console.log('Why am I seeing this log?');
res.end();
})
Lets have another route
app.get('/test', (req, res) => {
console.log('Test called');
res.end();
})
Now on browser localhost:port/test
Now which route will call.
As per your code it will consider that you are calling default route, where your first two argument is optioal.
So always default route will called. and test route skipped. Because test is now parameter of default route. Not another route
Think deeply Either test route work or default route
Hi I am facing the problem that I need to log each incomming request and the associated responses in my database. My current solution looks like the following:
./routes/customer.js
router.get('/', async (req, res, next) => {
req.allCustomers = await fetchAllCustomers();
res.status(200).send(req.allCustomers);
next(); // <- this is my personal problem
});
./middleware/logging.js
module.exports = function (req, res, next) {
db.query(
`INSERT INTO logging SET ?`,
{
request: JSON.stringify([req.body, req.params]),
response: JSON.stringify(req.response)
}
);
}
routes declaration
module.exports = function(app) {
app.use(express.json());
app.use('/api/customers', customers); // <- ROUTE ./routes/customer.js
app.use(logging); // <- MIDDLEWARE ./middleware/logging.js
}
I already mentioned my problem in my first piece of code. It is really repetitive to call next() in every route manually and I would like to avoid this. I already tried to load the middleware before all routes, call next() in the middleware function and execute my db query afterwards but I do not have the response at this point because of the async functionality.
Is there any way to handle this situation or will I need keep calling next() at the end of each route function?
If you don't want to call next() from your routes, you cannot have middleware run after them. It needs to be placed before. But can you get the response inside a middleware that runs before the route? The answer is yes!
It may be a little hacky, but since your route uses res.send(), you can use that to your advantage. By running before your route, your middleware can hijack that res.send function, to make it do other stuff.
./routes/customer.js
router.get('/', async (req, res, next) => {
req.allCustomers = await fetchAllCustomers();
res.send(req.allCustomers); // We'll hijack this
});
./middleware/logging.js
module.exports = function (shouldBeLoggedFunc) {
return function (req, res, next) {
if (shouldBeLoggedFunc(req)) {
// Store the original send method
const _send = res.send;
// Override it
res.send = function (body) {
// Reset it
res.send = _send;
// Actually send the response
res.send(body);
// Log it (console.log for the demo)
console.log(`INSERT INTO logging SET ?`, {
request: JSON.stringify([req.body, req.params]),
response: JSON.stringify(body)
});
};
}
next();
};
};
routes declaration
function shouldBeLogged(req) {
// Here, check the route and method and decide whether you want to log it
console.log(req.method, req.path); // e.g. GET /api/customers
return true;
}
module.exports = function(app) {
app.use(express.json());
app.use(logging(shouldBeLogged)); // <- Place this before your routes
app.use('/api/customers', customers);
};
when you use express.Router class like you already did and then use this code
app.use('/api/customers', customers);
you don't have to write 'next()' inside callback function in router.get .
there is an example
create a router file named birds.js in the app directory, with the following content:
var express = require('express')
var router = express.Router()
// middleware that is specific to this router
router.use(function timeLog (req, res, next) {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', function (req, res) {
res.send('Birds home page')
})
// define the about route
router.get('/about', function (req, res) {
res.send('About birds')
})
module.exports = router
Then, load the router module in the app:
var birds = require('./birds')
// ...
app.use('/birds', birds)
I'm trying to write the very simple nodejs express application, but it fails on routing request with query parameters. Here is a a very short code:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('hello express');
});
app.get('/user/:id', (req, res) => {
res.send(req.params.id);
});
app.listen(3000, () => {
console.log('Start listening at 3000');
});
The second "get" returns "Cannot GET /user".
What I'm doing wrong?
The second GET expects something like /user/1, /user/arup etc. The :id has to be replaced by something and it is required part of the url. That is why only /user gives you 404. This is called dynamic url.
The route /user/:id is not expecting query parameters but rather route parameters. Meaning it is expecting /user/4384 not /user?id=4384
You should try:
app.get('/user' && id, (req, res) => {
res.send(req.params.id);
});
Trying to get URl parameters in express js,but got empty object.
var password= require('./routes/password');
app.use('/reset/:token',password);
password.js
router.get('/', function(req, res, next) {
console.log(req.params);
res.send(req.params);
});
console.log(req.params) output is {}
Access url :http://localhost:3000/reset/CiVv6U9HUPlES3i0eUsNwK9zb7xVZpfHsQNuzMNWqLlGA4NJKoagwbcyiUZ8
By default, nested routers do not get passed any parameters that are used in mountpaths from their parent routers.
In your case, app is the parent router, which uses /reset/:token as a mountpath, and router is the nested router.
If you want router to be able to access req.params.token, create it as follows:
let router = express.Router({ mergeParams : true });
Documented here.
You are getting params and query mixed up.
Query approach
Your code should look like this when using query values for the example url: www.example.com?token=123&foo=bar
router.get('/', function(req, res, next) {
console.log(req.query);
console.log(req.query.token); // to log value of token
console.log(req.query.foo); // to log value of foo
res.send(req.query);
});
Params approach
Your code should look like this when using params values for the example url: www.example.com/123
router.get('/:token', function(req, res, next) {
console.log(req.params);
console.log(req.params.token); // to log value of token
res.send(req.params);
});
Instead you can use a middleware to log the path params:
const logger = (req, res, next)=>{
console.log(req.params)
res.send(req.params)
next()//<----very important to call it.
};
app.use(logger); //<----use to apply in the app
router.get('/', (req, res, next)=>res.send('Logged.'));
Actually you messed it up a little bit. You have to pass instance of express to your module.
Server.js:
//adding modules
require('./routes/password')(app);
Password.js:
module.exports = function(router) {
router.get('/reset/:token', function(req, res, next) {
console.log(req.params);
res.send(req.params);
});
//and so on.. your routes go here
}