I am currently working on a project with node where I need to use module.exports for my functions exports. I export the showMessage function like this:
const showMessage = (req, res) => {
res.status(200).send(`Message: ${req.param.message}`);
}
module.exports = {
showMessage
}
And import it here:
const express = require('express');
const router = express.Router();
const { showMessage } = require('../controllers/auth');
router.get("/:message", showMessage);
module.exports = router;
And further here:
fs.readdirSync("./routes").map((r) => app.use("/api", require(`./routes/${r}`)));
My project is supposed to take message from the api and show it on the webpage. E.g.: If I go to http://localhost:8000/api/good-morning, I should get on my webpage: Message: good-morning Instead I get Message: undefined. This makes me think that there is a problem with the scope of (req,res) parameteres passed in the showMessage function.
I tried using import <> from <> syntax, but didn't work
CodePudding user response:
It should be req.params
instead of req.param
. Here are the API docs: https://expressjs.com/en/4x/api.html#req.params