如何在节点 js 中传递多个搜索参数 URL

How do I pass multiple search parameters in node js URL

我正在使用 React、Node、Express、Postgress。我将如何使用快速路由传递多个参数?例如:

这是我的路线:

//Get entries from materials2 table
app.get("/materials2/:id1&:id2", async (req, res) => {
    try {

        const {material_thickness, material_width} = req.params;
        const contact = await pool.query(
            `SELECT *
            FROM materials_inventory
            WHERE
                (material_thickness = ) AND
                (material_width BETWEEN (-0.5) AND (+0.5))`, [material_thickness, material_width]); 


        res.json(contact.rows[0]);

    } catch (err) {
        console.error(err.message);
    }
})

我的想法是我需要将两个参数传递给搜索查询

从反应方面我有:

const stateSetter = async (thickness, width, length, length2) => {
        try {
            
            const response = await fetch(`http://localhost:5000/materials/${thickness, width}`, [thickness, width])
            const jsonData = await response.json();

            console.log(thickness);

        } catch (err) {
            console.log(err.message)
        }
    }

所以想法是 stateSetter 接收一些值,将这些值与路由一起使用。我该怎么做?谢谢!

您可以使用 request query。像这样更改节点中的路由:

//Get entries from materials2 table
app.get("/materials2", async (req, res) => {
    try {

        const {materialThickness, materialWidth} = req.query;
        const contact = await pool.query(
            `SELECT *
            FROM materials_inventory
            WHERE
            (material_thickness = ) AND
            (material_width BETWEEN (-0.5) AND (+0.5))`, [materialThickness, materialWidth]); 


        res.json(contact.rows[0]);

    } catch (err) {
        console.error(err.message);
    }
})

然后你像这样在前端调用:

const stateSetter = async (thickness, width, length, length2) => {
    try {
        
        const response = await fetch(`http://localhost:5000/materials2?materialThickness=${thickness}&materialWidth=${width}`)
        const jsonData = await response.json();

        console.log(thickness);

    } catch (err) {
        console.log(err.message)
    }
}

此外,您从前端调用端点 materials,但在节点中将其指定为 materials2。我在答案中更新了它,但请记住。