我想点击链接到解决方案页面,但我的路线不起作用,出了什么问题?
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 expressLayouts =
require('express-ejs-layouts');
var index = require('./routes/index'); var users =
require('./routes/users');
var app = express();
app.set('layout', 'layout/layout'); app.set('view engine', 'ejs');
app.use(expressLayouts);
app.use(logger('dev')); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(express.static(path.joindirname,
'public')));
app.use('/', index); app.use('/users', users); app.use('/solution',
index);
2.routes /索引 var express = require('express'); var router = express.Router();
router.get('/', function(req, res) {
res.render('pages/index'});
});
router.get('/solution', function(req, res) {
res.render('pages/solution');
});
module.exports = router;
答案 0 :(得分:1)
只需使用此代码
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 expressLayouts = require('express-ejs-layouts');
var router = express.Router(); // need to add
var index = require('./routes/index')(router); /* need to pass router object */
var users = require('./routes/users')(router); /* need to pass router object */
var app = express();
app.set('layout', 'layout/layout');
app.set('view engine', 'ejs');
app.use(expressLayouts);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.joindirname,'public')));
app.use('/', index);
app.use('/users', users);
在./routes/index文件中更新以下代码
module.exports = function (router) {
router.get('/', function(req, res) {
res.render('pages/index'});
});
router.get('/solution', function(req, res) {
res.render('pages/solution');
});
};