Created
September 17, 2017 19:21
-
-
Save retgits/47ac20261678c32f843660c8b8c6f76e to your computer and use it in GitHub Desktop.
Response to question on https://retgits.github.io/monitoring-your-node-js-apps-with-prometheus/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var url = require('url'); | |
router.get('/api', function(req, res, next) { | |
var err = "this is error"; | |
var data = { data: "Succuss"} | |
if (err) { | |
var error = Error (err) | |
next(error.message) | |
/** | |
* I've added the below two lines to make the code work as you expected it (or at least how I think you wanted it to work :)) | |
* res.statusCode sets the HTTP Status code, in this case to 400, so Prometheus can use that to differentiate between the various codes | |
* the original code would always result in statusCode 304. | |
* To add the status message you can simply do 'res.message = err' (see below). This will create a new key in the res object that your Prometheus | |
* method will use. | |
*/ | |
res.statusCode = 400; | |
res.message = err | |
res.json({message:err}) | |
}else { | |
res.json(data); | |
} | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment