从查询string中提取URL

考虑一个这样的url:

http://some-site.com/something/ http://www.some-other-site.com

我试图从查询string,即第二个http://使用以下方法login到控制台的粗体部分。

app.get("/something/:qstr",function(req,res){ console.log(req.params.qstr); }; 

然而,这只会工作,直到http: – >遇到/ /遇到它不再包含在req.params.qstr我想知道如何获取整个URLstring。 我怎样才能做到这一点?

谢谢。

你可以试试这个,使用正则expression式:

 var app = require('express')(); app.get(/^\/something\/(.*)/, function (req, res) { console.log(req.params[0]); res.json({ok: true}); }); app.listen(3333, () => console.log('Listening on 3333')); 

当你运行:

 curl http://localhost:3333/something/http://www.some-other-site.com 

服务器打印:

 http://www.some-other-site.com 

如你所想。

res.json({ok: true}); 是否只有返回一些回应,所以curl不会永远挂起。