javascript - 快速获取根事件不起作用

标签 javascript node.js http express url-routing

当我请求 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/

相关文章:

javascript - NodeJs 压力测试工具/方法

javascript - AngularJs $http.get : Sending array of objects as params

JavaScript/jquery ajax : what happens with inputs having same name attribute

javascript - 我不能在我的数组中使用 .push 和 Angular 吗?无法读取未定义的属性 jsonArr

javascript - 阻止加载 JS 文件的弹出窗口

javascript - Mongoose 回调挂起 Node js

html - 如果在 <iframe> 中单击链接,HTTP Referer 是什么?

javascript - Laravel 广播 : private channels not working due to auth error

javascript - 我如何使用事件发射器接收多个相同类型的事件?

node.js - 如何在 ReactJS 应用程序中为 markdown/LaTex 代码添加预览功能