我想在express js中获取路线的URL。
http://localhost:9310/api/arsys/v1/entry/SimpleForm5/?fields=Request ID,Assigned To,Status,Submitter,Create Date&q=Status=New&offset=5&sort=Create Date.desc
我只想要http://localhost:9310/api/arsys/v1/entry/SimpleForm5/
我尝试了const url = req.protocol + '://' + req.headers.host + req.url;
的{{1}}
但是它不给出http://localhost:9310/SimpleForm5/?fields=Request%20ID,Assigned%20To,Status,Submitter,Create%20Date&q=Status=New&offset=5&sort=Create%20Date.desc
。我也不希望在输出中使用查询参数。
请帮助
答案 0 :(得分:1)
要在没有queryParams的情况下检索url,但是在主机为originalUrl的情况下,可以采用以下方式:
const urlWithoutQueryParams = req.protocol + '://' + req.headers.host + url.parse(req.originalUrl).pathname;
例如,考虑该代码:
router.route('/arsys/v1/entry/SimpleForm5/')
.get(async (req, res) => {
try {
console.log(req.protocol + '://' + req.headers.host + url.parse(req.originalUrl).pathname)
return res.status(200).send({ message: "OK" });
} catch (error) {
return res.status(500).send({ message: "Failure" });
}
});
app.use('/api', router);
app.listen(8080, () => {
log.info('app started')
})
当您将GET
发送至:
http://localhost:8080/api/arsys/v1/entry/SimpleForm5/?fields=Request ID,Assigned To,Status,Submitter,Create Date&q=Status=New&offset=5&sort=Create Date.desc
结果是:
http://localhost:8080/api/arsys/v1/entry/SimpleForm5/