简而言之,我在公司DMZ中运行某些NodeJs应用程序,它基本上接收来自移动设备的调用,并将此类调用转发给在另一台服务器(超出我们的DMZ层的WebSphere)中运行的其他休息服务。
我一直在阅读很多关于NodeJs与Java不同的行为(我的意思是,默认的异步NodeJs行为与默认的Java同步行为)。您可以想象,我对NodeJs日志最佳实践有点新鲜。
对我来说非常清楚一些拇指规则,例如“不要在异步代码中使用try / catch"”。尽管如此,如果我至少做一个可接受的解决方案(参见我的代码末尾),我无法判断接近代码的最佳想法是什么。
为了更多地了解我的限制,我不知道如果这种代码的和平是异步的还是同步的,那么判断是否正确。好吧,我想它是同步的,因为我没有使用回调函数,也没有承诺库。
顺便问一下,我应该使用某种尝试/捕获方法或将其包装在域中吗?如果是这样,任何想法将如何高度赞赏。
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var router = express.Router();
var tokenRoute = router.route('/token');
//bellow one of my NodeJs rest service which basically receives a call from mobile and call forward to certain Spring application to answer back a token
tokenRoute.post(function (req, res) {
var bilhetagem = {
cpf: req.body.username,
servico: 'login'
};
logstash.send(bilhetagem);
var dataAuth = 'grant_type=' + req.body.grant_type +
'&username=' + req.body.username + '&password=' + req.body.password;
var auth = req.headers.authorization;
var args = {
data: dataAuth,
headers: { "Content-Type": "application/x-www-form-urlencoded", 'Authorization': auth }
};
var backend = ... here it is the rest service from our WebSphere beyond DMZ layer
client.registerMethod("postMethod", backend, "POST");
//here relies the main part of doubt. Let's say there is a predictable exception (eg. timeout). What is the best aprroach here?
client.methods.postMethod(args, function (data, response) {
res.writeHead(200, { "Content-Type": "application/json" });
var json = JSON.stringify({
tokenBackEnd: data
});
res.end(json);
});
});
app.use('/', router);
https.createServer(options, app).listen(config.get('node-porta'));
//here it is my current solution and it does work but I am wondering if I am realy doing a good practice over here.
//PS. I wrote below console.log just to focus on my question but certainly I am logging in file and so on
process.on('uncaughtException', function (err) {
console.log("process.on(uncaughtException)",err.message);
});