我写了一个名为“app.js”的服务器,它显示了我网站的很多页面。
var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
// routes
var route = require('./route');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
server.listen(3000,function(){
console.log("server listen at localhost:3000");});
app.get('/', route.index);
route.js:
var index = function(req, res, next) {
if(!req.isAuthenticated()) {
res.redirect('/index');
} else {
var user = req.user;
if(user !== undefined) {
user = user.toJSON();
}
res.render('menu', {title: 'Home', user: user});
}
};
module.exports.index = index;
项目文件夹:
/interface
/views
index.ejs
app.js
route.js
我该如何解决?我的计划中有什么错误?
答案 0 :(得分:0)
app.get
之后无法访问server.listen
。
放在它之前:
<强> app.js:强>
var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
// routes
var route = require('./route');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// here:
app.get('/', route.index);
server.listen(3000,function(){
console.log("server listen at localhost:3000");});
// app.get('/', route.index); <= not here
未经过身份验证时,重定向不是您想要的,但是:
<强> route.js:强>
// res.redirect('/index');
res.render('index');