从查询字符串中提取 URL
Extracting the URL from the query string
考虑这样的 url:
http://some-site.com/something/http://www.some-other-site.com
我正在尝试使用以下方法将查询字符串中的 bold 部分(即第二个 http://)登录到控制台。
app.get("/something/:qstr",function(req,res){
console.log(req.params.qstr);
};
但是这只会在 http: 之前有效 --> 一旦遇到 // 它就不再包含在 req.params.qstr
我想知道如何获得整个 URL 字符串。我怎样才能做到这一点?
谢谢。
您可以尝试使用正则表达式:
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});
仅用于 return 某些响应,因此 curl
不会永远挂起。
考虑这样的 url:
http://some-site.com/something/http://www.some-other-site.com
我正在尝试使用以下方法将查询字符串中的 bold 部分(即第二个 http://)登录到控制台。
app.get("/something/:qstr",function(req,res){
console.log(req.params.qstr);
};
但是这只会在 http: 之前有效 --> 一旦遇到 // 它就不再包含在 req.params.qstr
我想知道如何获得整个 URL 字符串。我怎样才能做到这一点?
谢谢。
您可以尝试使用正则表达式:
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});
仅用于 return 某些响应,因此 curl
不会永远挂起。