表达get root事件不起作用

时间:2015-08-07 15:55:08

标签: javascript node.js http express url-routing

当我请求 localhost:8080 / chat?k = 1& d = 1 时,它会将“CHAT PAGE”写入控制台并正常工作。但是当我试图获取 localhost:8080 root事件时,不写“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');

1 个答案:

答案 0 :(得分:2)

你的快递版似乎已经过时了。您必须将其更新到最新版本。在控制台中执行以下命令:

npm install express@latest

您还必须将res.sendfile("static/error.html")更改为res.sendFile(path.join(__dirname, "static/error.html")),将req.param("d")更改为req.params.d