最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to pass a url as a "url parameter" in express? - Stack Overflow

programmeradmin1浏览0评论

In my express app I have a router listening to api/shorten/:

    router.get('api/shorten/:longUrl', function(req, res, next) {
        console.log(req.params.longUrl);
    }

When I use something like:

http://localhost:3000/api/shorten/www.udemy

I get www.udemy which is what I expect.

But when I use:

http://localhost:3000/api/shorten/

I get a 404 error.

I want to get when I access req.params.parameter.

In my express app I have a router listening to api/shorten/:

    router.get('api/shorten/:longUrl', function(req, res, next) {
        console.log(req.params.longUrl);
    }

When I use something like:

http://localhost:3000/api/shorten/www.udemy.com

I get www.udemy.com which is what I expect.

But when I use:

http://localhost:3000/api/shorten/http://www.udemy.com

I get a 404 error.

I want to get http://www.udemy.com when I access req.params.parameter.

Share Improve this question asked Mar 24, 2017 at 19:47 Abdelaziz MokhnacheAbdelaziz Mokhnache 4,3493 gold badges27 silver badges35 bronze badges 7
  • You must pass url as body or at least in query param and not appending to the original url itself. The error you are getting is because browser will filter it out – binariedMe Commented Mar 24, 2017 at 19:50
  • You have to encode the URL you are attaching as characters like : are not allowed anywhere except in the protocol so can't be used in the path of the URL. – jfriend00 Commented Mar 24, 2017 at 19:50
  • 1 @binariedMe - That's the not true. It can be in the path if encoded properly. – jfriend00 Commented Mar 24, 2017 at 19:51
  • Agreed. I personally won't prefer encoding as you need to recreate url on server side. Instead passing this as query param or in body is easy – binariedMe Commented Mar 24, 2017 at 19:52
  • 1 Those are not legal URIs. The : character must be encoded when it appears anywhere other than in the first protocol. See section 2.2. of the RFC. – jfriend00 Commented Mar 24, 2017 at 20:21
 |  Show 2 more comments

4 Answers 4

Reset to default 22

I'm not sure if you're still looking for a solution to this problem. Perhaps just in case someone else is trying to figure out the same thing, this is a simple solution to your problem:

app.get('/new/*', function(req, res) {

  // Grab params that are attached on the end of the /new/ route
  var url = req.params[0];

This way you don't have to sweat about any forward slashes being mistaken for routes or directories, it will grab everything after /new/.

You need to use encodeURIComponent in the client, and decodeURIComponent in the express server, this will encode all the not allowed characters from the url parameter like : and /

You need to escape as so:

escape("http://www.google.com")

Which returns:

"http%3A//www.google.com"

I just want to add that if you pass another params like ?param=some_param into your "url paramter" it will not show up in req.params[0]. Instead you can just use req.url property.

发布评论

评论列表(0)

  1. 暂无评论