I am new on Express. I have the following code:
const { response } = require("express");
const express = require("express");
const app = express();
app.get("/api/products/:id", function (req, res) {
const id = req.params.id;
if (id === 1) {
res.json(id);
}
});
However, every time I set the conditional if the server don't respond and just stay processing. Am I doing something wrong?
CodePudding user response:
req.params are always strings;
app.get("/api/products/:id", function (req, res) {
const id = req.params.id;
if (id === '1') { // look here, the single quotes around 1 -> '1'
res.json(id);
} else {
res.json({message: 'Wrong param'}) // respond also if id is not '1', otherwise the client will still wait for a response when param is different by '1'
}
})