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

javascript - Get rest of the path from requested route in nodejs - Stack Overflow

programmeradmin7浏览0评论

Can you get the rest of the path in node.js from reuqested route using express?

Assuming I have my server on port 8080 and I just visit :8080/get/path/to/file

var url = require("url");
app.get("/get/*", function(req, res) {
  console.log(req.path);

  // this will return
  // '/get/path/to/file'

  console.log(url.parse(req.path);

  // this will return
  // protocol: null,
  // slashes: null,
  // auth: null,
  // host: null,
  // port: null,
  // hostname: null,
  // hash: null,
  // search: null,
  // query: null,
  // pathname: '/get/path/to/file',
  // path: '/get/path/to/file',
  // href: '/get/path/to/file' }
});

What I want here is to return path/to/file is there a way to get that? or could it be my app.get() route is wrong here?

I know that there are ways to do it using regex, split, substring and many other ways using plain JavaScript, but just want to see the best way to go for this.

Can you get the rest of the path in node.js from reuqested route using express?

Assuming I have my server on port 8080 and I just visit http://example.:8080/get/path/to/file

var url = require("url");
app.get("/get/*", function(req, res) {
  console.log(req.path);

  // this will return
  // '/get/path/to/file'

  console.log(url.parse(req.path);

  // this will return
  // protocol: null,
  // slashes: null,
  // auth: null,
  // host: null,
  // port: null,
  // hostname: null,
  // hash: null,
  // search: null,
  // query: null,
  // pathname: '/get/path/to/file',
  // path: '/get/path/to/file',
  // href: '/get/path/to/file' }
});

What I want here is to return path/to/file is there a way to get that? or could it be my app.get() route is wrong here?

I know that there are ways to do it using regex, split, substring and many other ways using plain JavaScript, but just want to see the best way to go for this.

Share Improve this question edited May 26, 2014 at 18:53 Ali asked May 26, 2014 at 18:43 AliAli 10.5k21 gold badges74 silver badges108 bronze badges 5
  • I'm not entirely sure how this works, did you mean app.get("/get/*", function(req, res) { ? – SomeKittens Commented May 26, 2014 at 18:54
  • So req.path contains the path you want right, what is it you're really asking for? Are you sure you're not just looking for variable URL's, as in app.get("/get/:path/:file", ... and then catch them in req.params.path etc – adeneo Commented May 26, 2014 at 18:57
  • @adeneo I want to be able to get anything after /get so if I visit http://example./get/this/is/what/i/want I should get back this/is/what/i/want – Ali Commented May 26, 2014 at 18:59
  • Then I'd say you should just do req.path.replace('/get',''), as that would be the easiest solution. There are probably ways to catch the asterisk in the route in params, just not quite sure how as I've never needed that. – adeneo Commented May 26, 2014 at 19:02
  • @adeneo I think your solution by far make the most sense and easy fix. Thanks! – Ali Commented May 26, 2014 at 19:07
Add a ment  | 

2 Answers 2

Reset to default 5

You can find path/to/file in req.params:

When a regular expression is used for the route definition, capture groups are provided in the array using req.params[N], where N is the nth capture group. This rule is applied to unnamed wild-card matches with string routes such as /file/*:

// GET /get/path/to/file
req.params[0]
// => path/to/file

I know this is a really old question, but I will post the answer for anyone searching for this. At the time of writing I am using express 4.18.2. To match the remainder of a route you can actually do something like this:

app.get("/some/url/:pathToFile(*)", (req, res) => {
  const pathToFile = req.params["pathToFile"]; //<- Holds the remainder of the route.
});

So navigating to /some/url/path/to/file will result in the route above being matched and the pathToFile variable will be "path/to/file".

发布评论

评论列表(0)

  1. 暂无评论