Home > Blockchain >  Simple NodeJS Express App Cannot get /url
Simple NodeJS Express App Cannot get /url

Time:11-10

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

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?

CodePudding user response:

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

  • Related