Reputation: 2003
Similar to this
I have the following route:
app.get('/blogpost/:link', middleware.loginCheck, blog.postBlogs);
Using req.params.link
returns the link parameter.
When I ask for the req.params.link
in middleware.loginCheck
I receive an undefined
.
It seems that req.params.link
is not being passed through to the middleware because I can access it like so:
app.get('/blogpost/:link', function(req, res){console.log(req.params.link)});
Whats the problem with my middleware?
FINAL
a(href='/post/#{post.link}') #{comments}
renders /post/myPostLink
{#post.link}
only renders the variable substitute and does not add an extra /
a(href='/post#{post.link}') #{comments}
renders '/postmyPostLink'
a(href='/post/' + post.link) #{comments}
renders /post/myPostLink
So both #{post.link}
or post.link
work the same and req.params.link
works on both calls.
UPDATE_2
I am using jade
to render the web page. Inside there I have an anchor tag written as so: a(href='/post/#{post.link}) #{comments}
The achor tag works fine and directs me to the correct page. But express
does not like this representation. Instead, if I change the anchor tag to a(href='/post/' + post.link) #{comments}
req.params.link
works fine.
UPDATE_1
req.param('link')
works for this case.
There is no problem with the middleware.
But why wouldn't req.params.link
work?
Upvotes: 1
Views: 1954
Reputation: 9407
In recent versions of express you can use router.param()
as your middleware to specifically work on the req.params
:
var router = express.Router();
router.param( "nickname", function (req, res, next, nickname) {
console.log( "nickname: ", nickname );
next();
});
router.get( "/user/:nickname", myController.method );
Upvotes: 0
Reputation: 145994
OK, we went back and forth in the comments a bit, but now my guess is that the problem is double slashes in your URL
a(href='/post/#{post.link}') #{comments}
That is A) missing a closing quote, but I assume that is a typo just in your question here, not in your code. and B) the real problem is that post.link
includes a leading slash, so you end up with this HTML:
<a href='/post//post_link_name'>42</a>
Thus express is parsing that as an empty :link
parameter. Change your jade to this:
a(href='/post#{post.link}') #{comments}
and you should get back in business.
Upvotes: 2
Reputation: 75656
what does the signature of middleware.loginCheck
look like? It needs to return a function(req, res, next)
Upvotes: -1