简体   繁体   中英

Express route not working - nodejs

I have a simple scenario. I am following Max tutorial.

My http://localhost:3000/message always returns index page. That is only the first route is working. The new route is not working. I am simply trying to put node.hbs on /message

/routes/app.js

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

router.get('/', function (req, res, next) {
    res.render('index');
});
router.get('/messsage', function (req, res, next) {
    res.render('node', { message: 'hello' });
});
module.exports = router;

app.js

var appRoutes = require('./routes/app');
app.use('/', appRoutes);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    return res.render('index');
});

Your code is working. The requested URL http://localhost:3000/message is not matching any of your declared paths so it is defaulting to your custom 404 page which is the same as your index page. Without changing your code and simply requesting http://localhost:3000/messsage will match the path of /messsage on your router. It's a typo. 😉

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM