总node.js的noobie,开始从各种教程和网站的演示代码玩,我注意到的东西,我不明白...node.js的app.get不会被调用
即,如果我有指数.html在我的/公用文件夹中,然后
app.get("/", function (req, res) {
console.log("get /");
res.redirect("/test.html");
});
根本就不会被调用。当我重新命名的index.html到index2.html然后调用方法时,我重定向到/public/test.html
这是我有:
var io = require('socket.io'),
express = require('express'),
MemoryStore = express.session.MemoryStore,
app = express.createServer(),
sessionStore = new MemoryStore();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
store: sessionStore,
secret: 'secret',
key: 'express.sid'
}));
app.use(express.static(__dirname + '/public'));
});
,其余是相当大部分来自本教程:http://www.danielbaulig.de/socket-ioexpress/
同样的问题出现在任何其他文件中。如果我有/public/test.html,然后当我打电话
http://localhost:8201/test.html
这app.get不叫:
app.get("/test.html", app.authenticateUser, function (req, res) {
console.log("get /test.html");
res.redirect("/test2.html");
});
当我删除的test.html然后我得到转发到TEST2。 html ...
我试图重定向的原因是,如果用户没有登录我不希望他打开index.html,而是想转发他到login.html,如果索引.html存在。唯一的“解决方案”是做它的客户端吸引,我不希望index.html加载到客户端浏览器只是为了将他转发到login.html,服务器应该,在我的看法,处理。
为什么你使用静态'index.html'而不是视图?在我看来,你只是让自己变得更加困难。 –