当我请求 localhost:8080/chat?k=1&d=1 时,它会将“CHAT PAGE”写入控制台并正常工作。但是,当我尝试获取 localhost:8080 时,根事件不会写入“INDEX PAGE”,并且它会自动获取 index.html,即使我将 index 设置为 false。这是我的代码;
var path = require('path');
var express = require('express');
var encData;
var appointmentKey;
var urlGetDataAreValid = false;
var app = express();
app.use(express.static(path.join(__dirname, 'static'), { index: false }));
app.get('/', function(req, res, next) {
console.log("INDEX PAGE");
res.sendFile('static/error.html');
});
app.get('/chat', function(req, res, next) {
console.log("CHAT PAGE");
encData = req.param('d');
appointmentKey = req.param('k');
if ((encData === undefined || encData === null) || (appointmentKey === undefined || appointmentKey === null) ) {
res.sendfile('static/error.html');
}
else {
urlGetDataAreValid = true;
res.sendfile('static/index.html');
}
});
var server = app.listen('8080');
最佳答案
您的 express 版本似乎已过时。您必须将其更新到最新版本。在您的控制台中执行此命令:
npm install express@latest
您还必须将 res.sendfile("static/error.html")
更改为 res.sendFile(path.join(__dirname, "static/error.html"))
和 req.param("d")
到 req.params.d
。
关于javascript - 快速获取根事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881969/