I'm new to NodeJs and express. My goal is to have a separate route file that contains all my application routes. So I created a file called routes.js
. The content looks like this:
const express = require('express');
const router = express.Router();
router.get('auth/register', require('./controllers/auth/register'));
module.exports = router;
In my main file, I require the file with this line. The app
variable contains my express instance.
app.use('/', require('./routes'));
If I call http://localhost/auth/register
in my browser, I'm always running into a 404 Not Found
error. When I define the route directly in the main file with app.get('/auth/register', require('./controllers/auth/register'));
it works well. There are no errors in my console.
Has anyone an idea why my sperate route file does not work? Thank you for your help!