Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 快速获取根事件不起作用_Javascript_Node.js_Http_Express_Url Routing - Fatal编程技术网

Javascript 快速获取根事件不起作用

Javascript 快速获取根事件不起作用,javascript,node.js,http,express,url-routing,Javascript,Node.js,Http,Express,Url Routing,当我请求localhost:8080/chat?k=1&d=1时,它会将“chat PAGE”写入控制台并正常工作。但当我试图获取localhost:8080根事件时,它并没有写入“INDEX PAGE”,即使我将INDEX设置为false,它也会自动获取INDEX.html。这是我的密码 var path = require('path'); var express = require('express'); var encData; var appointmentKey; var urlG

当我请求localhost:8080/chat?k=1&d=1时,它会将“chat PAGE”写入控制台并正常工作。但当我试图获取localhost:8080根事件时,它并没有写入“INDEX PAGE”,即使我将INDEX设置为false,它也会自动获取INDEX.html。这是我的密码

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(\uu dirname,“static/error.html”)
req.param(“d”)
更改为
req.params.d
,您的代码对我有效,但是,它在第14行的
res.sendFile
处显示了一个错误:
TypeError:path必须是绝对的,或者指定res.sendFile的根目录
您应该使用
req.params.d
req.params.k
而不是
req.param('d')
res.sendfile
已弃用,您应该使用
res.sendfile
@maxkl它不会给我错误信息。它只显示“index.html”。您使用的是哪个版本的express?让我们看看。