1

您好,我正在关注一些关于 node.js 和 experss.js 的教程。到目前为止,我构建的是以下代码。我的问题是,当我请求一个页面时: http://localhost:3000/test它确实按预期返回 404。/ip 和 /worker 也可以正常工作。但是当我请求例如http://localhost:3000/asdf/asdf浏览器返回Cannot GET /asdf/asdf

这是来源:

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

有人可以解释为什么它不返回 404,我应该怎么做才能返回 404?

更新

另外,当我要求http://localhost:3000我得到Cannot GET /

4

1 回答 1

0

这是我提出的解决方案:如何将 404 错误重定向到 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!');
}
于 2013-09-02T13:29:15.867 回答