在 URL 中发送数据时无法进入 GET 请求

问题描述

我正在使用邮递员测试我的 API,当我尝试在 URL 中发送数据时,邮递员响应 404 Not found。我猜它的格式,但四处搜索我认为它是正确的。谁能给点灯?

但是当我不在 URL 中发送任何数据时,它工作得很好:

当我尝试在 get 请求中发送数据时:

enter image description here

app.get("/mediciones/sigfox_libelium/:{device}",(req,res) => {
    const { device } = req.params;
    res.json("im in");
    console.log("Im in");
    console.log(device);
    console.log(req.params.device);

当我没有发送任何数据时,请求工作正常(它崩溃了,但因为它还没有完成):

enter image description here

app.get("/mediciones/sigfox_libelium",res) => {
    const { device} = req.params;
    res.json("im in");
    console.log("Im in");
    console.log(device);
    console.log(req.params.device);

解决方法

我猜您是在使用 Express.js 来构建 API,对吗?
我认为您以错误的方式使用 req.params

如果你想要参数名称device
您必须在 url 路径中使用 :device 而不是 :{device}。 像这样

app.get("/mediciones/sigfox_libelium/:device",(req,res) => {
    cosnt { device } = req.params // You can get device here
})