Search code examples
javascriptnode.jshttpexpressurl-routing

express get root event does not work


When I request localhost:8080/chat?k=1&d=1 it writes "CHAT PAGE" to console and works correctly. But when I tried to get localhost:8080 root event does not write "INDEX PAGE" and it automatically gets index.html even though I set index to false. Here is my code;

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');

Solution

  • Your version of express seems to be outdated. You have to update it to the latest version. Execute this command in your console:

    npm install express@latest
    

    You also have to change res.sendfile("static/error.html") to res.sendFile(path.join(__dirname, "static/error.html")) and req.param("d") to req.params.d.