将 url 作为 GET 请求的参数
Having a url as a parameter of a GET request
对于我的节点应用程序,我需要用户能够在获取请求中将 url 作为参数传递。
我试过将 url 作为“?”之后的参数但是好像还是不行。
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get("/create/:id")
这是我正在使用的代码(减去回调),如果 url 是 "example.com/create/google.com/images" 而不是路由,则参数 ID 应该变为 "google.com/images"未被调用。
您的代码是正确的。
像这样形成一个请求
[http://localhost:3006/create/2?q=https://twitter.com/]
在上面url id => 2, q => https://twitter.com
在req.query你会得到{"q":"https://twitter.com/"}
。
对于我的节点应用程序,我需要用户能够在获取请求中将 url 作为参数传递。
我试过将 url 作为“?”之后的参数但是好像还是不行。
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get("/create/:id")
这是我正在使用的代码(减去回调),如果 url 是 "example.com/create/google.com/images" 而不是路由,则参数 ID 应该变为 "google.com/images"未被调用。
您的代码是正确的。
像这样形成一个请求
[http://localhost:3006/create/2?q=https://twitter.com/]
在上面url id => 2, q => https://twitter.com
在req.query你会得到{"q":"https://twitter.com/"}
。