newbie问题如下:
我很难让EventEmitter工作。我已经考虑了该文档和几个最佳主教教程:
问题是,在用户中。userroutes.js不会触发任何user.on.on(),我真的不知道为什么..
任何建议都将受到赞赏。提示也可以更好地结构。我的目标是集中式端点(可读),并在不同模块中为每个模块(例如用户或产品/订单)重复使用代码,例如致电以更新用户端点的订单。
所以,假设,您的node.js服务器具有模块配置,路由器和几个类,例如:
/server.js
global.__base = __dirname + "/";
var server = require("http").createServer();
var routes = require("./v2");
var initialize = require(global.__base + "config");
var app = initialize.globalModules();
app.use('/api/v2', routes);
app.listen('8090');
/config.js
var db;
module.exports = {
globalModules: function() {
// load the global modules
var app = require("express")();
var bodyParser = require("body-parser");
//setUp app
app.use(bodyParser.json());
return app;
},
db: function() {
var mysql = require('mysql');
var db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'node_docx'
});
db.connect(function(err) {
if (err) throw err;
});
return db;
}
};
/v2/index.js(路由)
var userRoutes = require('./userRoutes');
var routes = require('express').Router();
routes.use("/user", userRoutes);
//routes.use("/product", productRoutes);
//routes.use("/order", orderRoutes);
//...
routes.use('*', function(req, res) {
res.status(404).send({code: 404, message: 'ERR', data: "Unknown target."});
});
module.exports = routes;
/v2/userroutes.js
var User = require("./routes/user.js");
var user = new User();
var route = require('express').Router();
route.get("/", function(req, res) {
user.on('error', function(err) {
res.status(400).send({code: 900, message: 'ERR', data: err});
});
user.on('failure', function() {
res.status(404).send({code: 901, message: 'ERR', data: "User not found!"});
});
user.on('success', function(result) {
res.status(200).send({code: 200, message: 'OK', data: result});
});
user.getAll();
});
module.exports = route;
/v2/routes/user.js
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var initialize = require(global.__base + "/config");
var db = initialize.db();
function User() {
EventEmitter.call(this); //edit: added, but has not solved the problem
var self = this; //solved it!
function _checkForErrors(error, rows, reason) {
if (error) {
self.emit('error', error);
return true;
}
if (rows.length < 1) {
self.emit('failure', reason);
return true;
}
return false;
}
function _getData(error, rows) {
if (_checkForErrors(error, rows)) {
return false;
} else {
self.emit('success', rows);
}
}
function getAll() {
db.query("SELECT * FROM patient", _getData);
}
this.getAll = getAll;
}
util.inherits(User, EventEmitter);
module.exports = User;
这很简单,您在函数User
中忘记了EventEmiter.call(this)
。
function User() {
EventEmitter.call(this);
var self = this;
function _checkForErrors(error, rows, reason) {
if (error) {
self.emit('error', error);
return true;
}
if (rows.length < 1) {
self.emit('failure', reason);
return true;
}
return false;
}
function _getData(error, rows) {
if (_checkForErrors(error, rows)) {
return false;
} else {
self.emit('success', rows);
}
}
function getAll() {
db.query("SELECT * FROM patient", _getData);
}
this.getAll = getAll;
}