Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simple NodeJS Express App Cannot get /url

I’ve been at this for about an hour now and I have no idea what’s wrong here. I can’t access the url.
I made this Simple NodeJs Express app but when I access http://localhost:3000/users/login It gives an error Cannot GET /users/login

This is my
app.js File.

const express = require('express');
const indexRouter = require('./routes/index');
const userRouter = require('./routes/users');
const app = express();
const port = process.env.PORT || 3000;


app.set('views', 'views');
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));


app.get('/', indexRouter);
app.get('/users', userRouter);

app.listen(port, () => console.log(`Server started on port ${port}`));

This is my routes/index.js file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.send("Welcome");
})

module.exports = router;

This is my routes/users.js file

const express = require('express');
const router = express.Router();

// Login Route
router.get('/login', (req, res) => {
    res.send("Login page");
})
// Register Route
router.get('/register', (req, res) => {
    res.send('Register Page');
})

module.exports = router;

Can anyone please tell me what’s the issue here?

>Solution :

Maybe instead of app.get('/users', userRouter) try app.use('/users', userRouter)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading