Reputation: 93
I am new to javascript and node.js I am learning online and trying to create a cms using node.js, express and sqlite. Before going to details I will give you guys my directory structure.
app.js is where the server is created and defaultRoutes are for routes. When I run
//code in defaultRoutes.js
const express = require('express');
const router = express.Router();
router.use((req,res) => {
res.render('default/index');
});
module.exports = router;
and
//code in app.js
//Routes
const defaultRoutes = require('./routes/defaultRoutes');
app.use(defaultRoutes);
the page renders but when I change the router.use in defaultRoutes to router.get the browser throws cannot GET error.
Can anyone explain why this is happening?
Upvotes: 0
Views: 124
Reputation: 1524
You need to define a route when using router.get()
. router.use()
can work for any route, but to define a route with router.get()
you can add the route like this:
//code in defaultRoutes.js
const express = require('express');
const router = express.Router();
router.get('/', (req,res) => {
res.render('default/index');
});
module.exports = router;
Upvotes: 1