您好我正在关注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 /
答案 0 :(得分:0)
这是我根据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!');
}