I am trying to seperate route from node.js server to folder routes. But it is not working.
When I run the server, console.log('hello from express') not printed . Also the res.send() is not executed.
routes/index.js
const express = require('express');
const router = express.Router();
router.get('/api/hello', (req, res) => {
console.log({ express: 'Hello From Express' });
res.send({ express: 'Hello From Express' });
});
module.exports = router;
and server.js
var indexRoute=require('./routes/index.js');
app.use('/api/hello',indexRoute);
How can I solve the problem? Thanks in advance.
/api/hello/api/hellofor your code to do anything.router.get('/', controller). This means that that controller path is routed from the parent path. In that case, the one defined in/api/hello. In you current code, access to:/api/hello/api/helloand should work. After my changes, you'll be able to access using/api/hello