How to Use Raygun to handle errors in Node. js applications
This article mainly introduces how to use Raygun to handle errors in Node. js applications. Node. js is a JavaScript framework for servers. For more information, see
Our raygun4node package provides a convenient way to send your Node. js error to Raygun. It can be easily installed using npm:
?
It can provide you with a raygun client, you can use it to configure your API key, and can be used to manually send error messages. but later you may say, "I don't want to manually send all the errors to Raygun. It sounds like there is a lot of work to do!" If you are using express. js, you can easily solve this worry with the express processor.
?
1 2 3 |
Var raygun = require ('raygun '); Var raygunClient = new raygun. Client (). init ({apiKey: 'Your API key '}); App. use (raygunClient. expressHandler ); |
In other cases, you may just want to use this method to listen for uncaught exceptions and send error messages.
?
1 2 3 4 5 |
Var raygun = require ('raygun '); Var raygunClient = new raygun. Client (). init ({apiKey: 'Your API key '}); Process. on ('uncaughtexception ', function (err ){ RaygunClient. send (err ); }); |
If you plan to start doing so, you must understand its meaning. however, when a time bubble keeps returning to the event loop, this event will be triggered. if you add a listener for this event, the default action will not happen again. when the default action is performed, the call stack information is printed and the process is exited. if you continue after triggering this operation, your node process will be in an unspecified state. node. the js document specifically mentions that you should not use this item, and it may be removed in the future. the recommended alternative is to use the domain domains. the following is a simple and small example. You can see how the raygun client is applicable to your domain.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Var domain = require ('domain '); Var raygun = require ('raygun '); Var raygunClient = new raygun. Client (). init ({apiKey: 'Your API key '}); Var server = require ('http'). createServer (function (req, res ){ Var d = domain. create (); D. on ('error', function (err ){ RaygunClient. send (err ); // Clean up and end }); D. add (req ); D. add (res ); D. run (function (){ // Handle the req, res }); }); Server. listen (3000 ); |
I hope this will help you better understand how to handle errors in Node. js of Raygun.