私のノード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 のような私のリクエスト URL
どうもありがとう。
エラーを追加:
(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)