【发布时间】:2014-11-14 17:08:03
【问题描述】:
我有来自https://github.com/chjj/tty.js/的以下代码:
this.get("/hola", function(res) {
iniciar();
});
function iniciar() {
self.init();
}
iniciar();
去localhost:8080/hola,它没有加载。 localhost:8080 完美运行。 self.init() 调用一个函数,该函数又调用其他函数。问题似乎是以下调用函数:
Server.prototype.initMiddleware = function() {
var self = this
, conf = this.conf;
this.use(function(req, res, next) {
var setHeader = res.setHeader;
res.setHeader = function(name) {
switch (name) {
case 'Cache-Control':
case 'Last-Modified':
case 'ETag':
return;
}
return setHeader.apply(res, arguments);
};
next();
});
this.use(function(req, res, next) {
return self._auth(req, res, next);
});
this.use(term.middleware());
this.use(this.app.router);
this.use(express.static(__dirname + '/../static'));
};
根据express.js 文档:
// a middleware sub-stack which prints request info for any type of HTTP request to /user/:id
app.use('/user/:id', function(req, res, next) {
console.log('Request URL:', req.originalUrl);
next();
}, function (req, res, next) {
console.log('Request Type:', req.method);
next();
});
因此,第一个 app.get 与其他 app.use 或 this.use 之间似乎存在“冲突”。我该如何解决?
【问题讨论】:
-
您真的只想让
localhost:8080/hola的行为与localhost:8080/一样吗?您可以简单地将static/index.html复制到static/hola中,然后将索引文件复制到另一个文件中。 Express 服务器直接从/static文件夹提供终端应用程序的静态 HTML;您正在修改的服务器端逻辑仅指定如何响应来自静态 HTML 客户端的请求,而不是如何将基本客户端代码发送到浏览器。 -
为了帮助你理解你的实际问题,
self.init()在这里设置了服务器端逻辑。基本上,您已经做到了,因此您需要在主页 (/) 工作之前访问/hola,但访问/hola本身并没有做任何其他事情(除了使应用程序能够在全部)。只是这甚至不是真的,因为无论如何你已经在app.get('hola')侦听器之外运行了self.init()(使用iniciar()),所以访问/hola完全没有任何作用。 -
@apsillers,关于您的评论,您建议
this.get("/", function(res) {iniciar();});并在app.get之外删除self.init会起作用。但事实并非如此。 -
执行以下操作:1. 将代码恢复原状(暂时)。您不需要更改服务器端逻辑来执行您想要执行的操作,并且可能会停止此表单的工作,直到您有一个可以确定工作的设置。 2. 将
static/index.html复制到一个新文件static/hola中。 3. 在浏览器中加载localhost:8080/hola。 -
好的,@apsillers,它不起作用。我的目标是为这个应用程序设置一个更安全的登录,例如passportjs.org。因此,当登录成功时,我需要护照以使用
get重定向用户。
标签: javascript node.js http express