문제

Here’s my routing :

router.route('/search:word').get(function(req, res) {
  var re = new RegExp(req.params.word, 'i');
  console.log(req.params.word);

  Place.find().or([{ 'title': { $regex: re }}, { 'category': { $regex: re }}]).sort('title').exec(function(err, places) {
    res.json(JSON.stringify(places));
  });
});

When I use the request /places/search:test the console displays :test instead of just "test". Any idea what’s happening?

도움이 되었습니까?

해결책

Do you really mean to use router.route('/search:word') instead of router.route('/search/:word')? Using colon there seems kind of odd.

If you use router.route('/search/:word'), and if your request is

/places/search/test

then you get req.params.word="test"

다른 팁

If you want same pattern api end point like eg: /places/search:test then in api endpoint you have to use double colon. Example -

router.route('/search::word').get(function(req, res) {
   console.log(req.params.word); ---> "test"
});

Thats why you are getting extra colon :test

What's happening here is that it's breaking down the path to /search[word]. So when you request /search:test, [word] matches the part after /search, which would be :test in that case.

What you probably need instead is something like: /search::word

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top