I cannot use route parameters with app.use on Express 4.16.2.
I have a statement like this:
app.use('/course-sessions/:courseSessionId/chapter-sessions', chapterSessionsRouter)
And, in chapter sessions router:
// Index.
router.get('/', ...resource.indexMethods)
When I got '/course-sessions/0/chapter-sessions/', I have 404.
Then I am trying a statement like this:
app.get('/course-sessions/:courseSessionId/chapter-sessions', ...chapterSessionResource.indexMethods)
Now I have the result I want. So route parameters don't work with app.use.
While I am researching, I got this GitHub issue and this pull request which closes the issue. But it seems that the issue is still around here. Or do I make something wrong?
You have to setup your router in a different way, try using mergeParams to access parameters in parent routes.
let router = express.Router({ mergeParams: true });
See docs: http://expressjs.com/en/api.html
I’d do only app.use('/course-sessions/', chapterSessionsRouter) and handle the id inside the router.
Related
I wanna use two routers express.static() but only "/" router apply express.static() function.
I don't know why. help me pls.. below this is my project folder.
app.use(express.static("src/assets"));
app.use("/", globalRouter);
app.use("/users", userRouter);
You need to flip your app.use statements
app.use("/users", userRouter); // Try to match 1st
app.use(express.static("src/assets")); // Try to match 2nd
app.use("/", globalRouter); // Try to match last
Express will now try to match from the /users route before matching from static, then if it cannot find a match in either /users or static it will then try the global router.
I´m trying to use a different set of routes for each vhost
module.exports = function(app) {
app.use(vhost('www.example.com', exampleRoutes))
app.use(vhost('*.example.com', subdomainRoutes))
}
My problem is that www.example is also using the routes from subdomainRoutes
I need to somehow specify that if I´m under www then only the exampleRoutes should work
update. looks like I can use a regexp. I will need something like
not(www.example.com) but I´m terrible with regexp :(
laving this here in case someone needs it.
const regex = new RegExp('^(?!.*www.example.*).*$');
app.use(vhost(regex, subdomainRoutes))
Hi i need some help for this issue, I need to run:
exports.someFunction = function () {
// i need call a route like this.
app.route('api/getdata');
};
I need call some route in express function. How can I do it?
I'm assuming that /api/getdata returns some sort of data (in a format like JSON) you need to use. In that case, it is pretty simple, just use Node's excellent unirest library. I find Node's http a little advanced for this case. Assuming that this is a GET request, it would look something along the lines of:
unirest.post('http://example.com/api/getdata').end(function (response) {
console.log(response.body);
});
Of course, you can use Node's http.get if you wanted or any other HTTP library you like.
I am a novice in expressjs and don't know much about routing and such concepts in expressjs. While reading about it, I saw that one can set a route like
route1 = app.get("/:param",callback)
where param will become the route param variable and all such get requests like: "/foo" or "/bar" will correspond to that route.
My question is: can I have a route now that is
route2 = app.get("/param", callback)
or
app.get("/anyOtherRoute",callback)
If so, how can I know that the request is for route1 and not for route2 (or vice versa)?
You don't, really. But you can define your routes in an order such that you have different behaviour for anyOtherRoute.
For example:
app.get('/anyOtherRoute', doFoo);
app.get('/:param', doBar);
If doFoo terminates the request without calling next() you'll get the separation I think you're looking for.
doFoo would be called first since express goes through the routes in the order that they are defined and added to the app.
This is essentially a continuation of the question here: Nodejs Passport display username.
app.get('/hello', function(req, res) {
res.render('index.jade', { name: req.user.username });
});
So users log in via PassportJS, and goes to index.jade, which contains #{name} in the body, which will be replaced by the value of req.user.username.
Question: Is it possible to use the value of req.user.username in index.jade's JavaScript? I tried assigning its value to a variable but it doesn't work.
I have been using the trick of having a hidden input with #{name} as value:
input(type='hidden', id='variableName', value='#{name}')
Then JavaScript can access this value using:
$("#variableName").val()
This works. But does it have any potential downside like security issues? What is the right way to do this?
You have a few options. One of them is what you did and put the value inside you html. You can also solve it by doing:
script
window.name = #{name};
This will create an inline script that sets the variable. The other option you have is using ajax. That means you probably need to make an extra route to reply to that request.