我的节点 js 就是这样,但是当我启动这个服务器时,它遇到了错误:
我的节点版本是 v0.6.18。我遵循建议,因为 可能检测到 EventEmitter 内存泄漏
添加 req.setMaxListeners(0) 但它不起作用。
我在这里附上我的整个代码:
var express = require('../node_modules/express'),
redis = require('../node_modules/socket.io/node_modules/redis'),
// io = require('../node_modules/socket.io').listen(app),
fs = require('fs');
var app = express.createServer();
app.use(express.bodyParser());
var cache = redis.createClient(7010, "192.168.100.18");
var port = 8888;
// get html page ok
app.get('/*', function (req, res) {
req.setMaxListeners(0);
res.sendfile(__dirname + '/html/' + req.params[0]);
});
// get js page ok
app.get('/js/*', function (req, res){
res.sendfile(__dirname + '/html/js/' + req.params[0]);
});
// get image ok
app.get('/img/*', function (req, res){
res.sendfile(__dirname + '/html/img/' + req.params[0]);
});
// login interface ok
app.post('/login*', function(req, res, next){
console.log("access login")
var username = req.body.username;
var password = req.body.password;
if(username == null || password == null){
console.log("username or password is nil")
res.json({ret:false})
return
}
cache.hget("margin:user:password", username, function(err, resp){
if (err != null){
console.log(err)
}
if(resp == null){
res.json({ret:false})
return
}
res.json({ret:true})
return
})
});
// upload file interface ok
app.post('/upload*', function(req, res, next){
console.log(req.files)
console.log(req.body)
fs.readFile(req.files.image.path, function(err, data){
var unix = Math.round(+new Date()/1000);
var imagename = unix + "." + req.files.image.type.split("/")[1]
var newPath = __dirname + "/html/img/" + imagename;
fs.writeFile(newPath, data, function(err){
if(err == null){
res.json({ret:true, img:"http://192.168.100.188:" + port + "/html/img/" + imagename});
return;
}
res.json({ret:false});
});
});
});
app.listen(port);
我的请求网址如 localhost:8888/index.html
多谢。
添加错误:
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace:
at Socket.<anonymous> (events.js:139:15)
at ServerResponse.sendfile (/data/www/margintop/server/node_modules/express/lib/response.js:185:14)
at /data/www/margintop/server/webserver/webserver.js:17:9
at callbacks (/data/www/margintop/server/node_modules/express/lib/router/index.js:165:11)
at param (/data/www/margintop/server/node_modules/express/lib/router/index.js:139:11)
at pass (/data/www/margintop/server/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/data/www/margintop/server/node_modules/express/lib/router/index.js:173:4)
at Object.router [as handle] (/data/www/margintop/server/node_modules/express/lib/router/index.js:33:10)
at next (/data/www/margintop/server/node_modules/express/node_modules/connect/lib/proto.js:190:15)
at multipart (/data/www/margintop/server/node_modules/express/node_modules/connect/lib/middleware/multipart.js:52:61)