In few words, I have certain NodeJs application running in my company DMZ which basically receives calls from a mobile and forward such calls to other rest services running in another server (WebSphere beyond our DMZ layer).
I have been reading a lot about how NodeJs behaves different from Java (I mean, the default asynchronous NodeJs behavior versus default Java synchronous behavior). As you can imagine, I am bit new to NodeJs logs best practices.
It is very clear for me some rules of thumbs like "don't use try/catch in asynchronous code". Nevertheless, I can't judge what would be the best idea to approach the code bellow neither if I am at least doing an acceptable solution bellow (see the end of my code).
To reveal a bit more about my limitation, I don't know judge properly if such peace of code is asynchronous or synchronous. Well, I guess it is synchronous because I didn't use call back functions neither promises library.
By the way, should I use certain try/catch approach bellow or wrap it in domain? If so, any idea how will be high appreciated.
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);
});