Syd Syd - 5 months ago 84
Node.js Question

node.js express routing 404 issue

Hello I'm following some tutorials regarding node.js & experss.js.
What I've built so far is the following code. My issue is that when I request a page such as:

http://localhost:3000/test
it does return 404 as expected. Also /ip and /worker work fine.
But when I request for example
http://localhost:3000/asdf/asdf
the browser returns
Cannot GET /asdf/asdf


Here is the source:

var cluster = require('cluster');

if (cluster.isMaster) {

var cpuCount = require('os').cpus().length;
var workers = [];

for (var i = 0; i < cpuCount; i++) {
workers[i] = cluster.fork();
console.log('\t - Starting Worker (PID): ' + workers[i].process.pid);
}

console.log('\n\t - ' + cpuCount + ' Workers started.\n');

cluster.on('exit', function (worker){

console.log('\t - Worker ' + worker.id + ' with (PID): ' + worker.process.pid + ' died!');

for (var i = 0; i < workers.length; i++) {

if (worker.process.pid === workers[i].process.pid) {
workers.splice(i, 1);
console.log('\t - Workers alive: ' + workers.length);
}

}

for (var i = 0; i < cpuCount - workers.length; i++) {

workers.push(cluster.fork());
console.log('\t - Starting Worker with (PID): ' + workers[workers.length - 1].process.pid);

}

});

} else {

var express = require('express');
var app = express();

app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);

app.get('/:page', function (req, res) {

var page = req.params.page;

switch(page) {
case '':
console.log('Page: ' + page);
res.sendfile(__dirname + '/index.html');
break;
case 'ip':
res.send('Client ip address: ' + req.ip);
break;
case 'worker':
res.send('Worker ' + cluster.worker.id + ', with (PID): ' + cluster.worker.process.pid + ' replyed.');
break;
default:
res.status(404).send('<h1>Page not found!</h1>');
}

});


console.log('\t - Worker ' + cluster.worker.id + ' up & running!');
}


Could someone please explain why it doesnt return a 404 and what should I do in order to return 404?

UPDATE

Also when I request
http://localhost:3000
I get
Cannot GET /

Syd Syd
Answer Source

This is the solution I came up with according to: How to redirect 404 errors to a page in ExpressJS?

var cluster = require('cluster');

if (cluster.isMaster) {

    var cpuCount = require('os').cpus().length;
    var workers = [];   

    for (var i = 0; i < cpuCount; i++) {
        workers[i] = cluster.fork();
        console.log('\t - Starting Worker (PID): ' + workers[i].process.pid);
    }

    console.log('\n\t - ' + cpuCount + ' Workers started.\n');

    cluster.on('exit', function (worker){

        console.log('\t - Worker ' + worker.id + ' with (PID): ' + worker.process.pid + ' died!');

        for (var i = 0; i < workers.length; i++) {

            if (worker.process.pid === workers[i].process.pid) {
                workers.splice(i, 1);
                console.log('\t - Workers alive: ' + workers.length);
            }

        }

        for (var i = 0; i < cpuCount - workers.length; i++) {

            workers.push(cluster.fork());
            console.log('\t - Starting Worker with (PID): ' + workers[workers.length - 1].process.pid);

        } 

    });

} else {

    var express = require('express');
    var app = express();

    app.configure(function() {
        app.set('views', __dirname + '/views');
        app.set('view engine', 'jade');

        app.use(express.bodyParser());
        app.use(express.methodOverride());
        app.use(app.router);
        app.use(express.static(__dirname + '/public'));
        app.use(function(req, res) {
            res.status(404).end('<h1>Page not found!</h1>');
        });
    });

    var server = require('http').createServer(app);
    var io = require('socket.io').listen(server);
    server.listen(3000);

    app.get('/', function (req, res) {
        res.sendfile(__dirname + '/index.html');
    });

    app.get('/:page', function (req, res) {

        var page = req.params.page;

        switch(page) {
            case 'ip':
                console.log('Case -> ip');
                res.send('Client ip address: ' + req.ip);
            break;
            case 'worker':
                console.log('Case -> Worker');
                res.send('Worker ' + cluster.worker.id + ', with (PID): ' + cluster.worker.process.pid + ' replyed.');
            break;
        }

    });

    console.log('\t - Worker ' + cluster.worker.id + ' up & running!');
}