node.js - ExpressJS router middleware cannot render views -
trying set routes using express router middleware. except index.jade ( @ path http:localhost:3000/ ), other template under views directory not being rendered , router fails send response set in module. below snippet of code:
app.js
var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); app.use(logger('dev')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({extended: false})); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); app.use(function(req, res, next) { var err = new error('not found'); err.status = 404; next(err); }); if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); }; app.use(function(err, req, res, next) { if (!err) { return next(); } res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); console.error(err.stack); }); module.exports = app;
routes/index.js
var express = require('express'); var router = express.router(); router.get('/', function(req, res, next) { res.render('index', { title: 'request' }); }); module.exports = router;
routes/users.js
var express = require('express'); var router = express.router(); router.get('/users', function(req, res, next) { res.send('respond resource'); }); module.exports = router;
inside routes/users.js use router.get('/users'
. believe should router.get('/'
. way have set handle requests http://localhost:3000/users/users.
Comments
Post a Comment