express-stats
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -8,36 +8,42 @@ 'use strict'; | ||
function createMethods(options) { | ||
if (!options) { | ||
options = { hostname: 'localhost', auth: noAuth(), logPath: logPath() }; | ||
} else { | ||
options.hostname = (!options.hostname ? 'localhost' : options.hostname); | ||
options.auth = (!options.auth ? noAuth() : options.auth); | ||
options.logPath = (!options.logPath ? logPath() : options.logPath); | ||
} | ||
function createRouter(options) { | ||
options = (!options ? {} : options); | ||
options.hostname = (!options.hostname ? 'localhost' : options.hostname); | ||
options.logPath = (!options.logPath ? logPath() : options.logPath); | ||
options.swaggerFile = (!options.swaggerFile ? 'swagger.json' : options.swagger); | ||
var router = express.Router(); | ||
// GET /system/logs/{logType} | ||
router.get('/system/logs/:logType', function (req, res) { | ||
var options = { headers: { 'Content-Type': 'text/html; charset=utf-8' }}; | ||
res.sendFile(logType(req.params.logType, options), options); | ||
// GET /logs/{logType} | ||
router.get('/logs/:logFile', function (req, res) { | ||
var file = options.logPath + req.params.logFile; | ||
var params = { headers: { 'Content-Type': 'text/html; charset=utf-8' }}; | ||
res.sendFile(file, params); | ||
}); | ||
// DELETE /system/logs/{logType} | ||
router.delete('/system/logs/:logType', function (req, res) { | ||
fs.writeFile(logType(req.params.logType, options), 'Nothing here', function (err) { | ||
if (err) { return res.status(500).send(err); } | ||
res.status(200).send('OK'); | ||
// DELETE /logs/{logFile} | ||
router.delete('/logs/:logFile', function (req, res) { | ||
var file = options.logPath + req.params.logFile; | ||
fs.exists(file, function (exists) { | ||
if (exists) { | ||
fs.writeFile(file, 'Nothing here', function (err) { | ||
if (err) { return res.status(500).send(err); } | ||
res.status(200).send('OK'); | ||
}); | ||
} else { | ||
res.status(404).send('Not found'); | ||
} | ||
}); | ||
}); | ||
// GET /system/swagger | ||
router.get('/system/swagger', function (req, res) { | ||
res.render('swagger.json', { hostname: options.hostname }); | ||
// GET /swagger | ||
router.get('/swagger', function (req, res) { | ||
res.render(options.swaggerFile, { hostname: options.hostname }); | ||
}); | ||
// GET /system/mem | ||
router.get('/system/mem', function (req, res) { | ||
// GET /mem | ||
router.get('/mem', function (req, res) { | ||
var mem = process.memoryUsage(); | ||
var used = (mem.heapUsed / mem.heapTotal).toFixed(2); | ||
res.send('Memeory usage is ' + used + ' %'); | ||
res.send('Memory usage is ' + used + ' %'); | ||
}); | ||
@@ -48,16 +54,2 @@ | ||
function logType (str, options) { | ||
if (str === 'err' || str === 'error') { | ||
return options.logPath + 'error.log'; | ||
} else if (str === 'for' || str === 'forever') { | ||
return options.logPath + 'forever.log'; | ||
} else return options.logPath + 'console.log'; | ||
} | ||
function noAuth () { | ||
return function (req, res, next) { | ||
next(); | ||
}; | ||
} | ||
function logPath() { | ||
@@ -67,2 +59,2 @@ return path.normalize(__dirname + '../../../../logs/'); | ||
module.exports = createMethods; | ||
module.exports = createRouter; |
{ | ||
"name": "express-stats", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Runtime statics for express production servers", | ||
@@ -5,0 +5,0 @@ "main": "app.js", |
# express-stats | ||
express-stats enables developers to watch their [express](https://www.npmjs.com/package/express) servers. These features are supported: | ||
express-stats enables developers to watch their [express](https://www.npmjs.com/package/express) production servers. These features are supported: | ||
* see log files, e.g. generated by [forever](https://github.com/foreverjs/forever) | ||
* render environment specific Swagger file | ||
* see log files, e.g. generated by [forever](https://github.com/foreverjs/forever) | ||
* see memory usage | ||
@@ -11,10 +11,8 @@ | ||
Before you start provide these files in the project folder and make the logs folder writeable (```chmod 700 -R logs```): | ||
Before you start provide log files in your project folder and make the logs folder writeable (```chmod 700 -R logs```), e.g.: | ||
* ```./views/swagger.json``` | ||
* ```./logs/forever.log``` | ||
* ```./logs/error.log``` | ||
* ```./logs/console.log``` | ||
express-stats depends on [cors](https://www.npmjs.com/package/cors) and [ejs](https://www.npmjs.com/package/ejs), so please provide in your app.js accordingly: | ||
express-stats depends on [cors](https://www.npmjs.com/package/cors) and [ejs](https://www.npmjs.com/package/ejs), so configure your Express app accordingly: | ||
@@ -27,4 +25,5 @@ ``` | ||
var options = { | ||
hostname: 'localhost:8080', | ||
logPath: __dirname + '/logs' | ||
hostname: 'localhost:8080', // default to 'localhost' | ||
logPath: __dirname + '/logs' // defaults to '/logs', | ||
swaggerFile: 'swagger.json' //defaults to 'swagger.json' | ||
} | ||
@@ -40,27 +39,11 @@ | ||
## Swagger | ||
## | ||
Get Swagger file. Your swagger file should contain the variable ```"host": "<%- hostname %>"```, so that Swagger provides a environment specific file. | ||
Get Swagger file. | ||
## Authentication | ||
## Swagger | ||
Optionally you can provide an authentication router, e.g.: | ||
Your swagger file should contain the variable ```"host": "<%- hostname %>"```, that may be generated environment specific. The following Swagger describes the provided endpoints. | ||
``` | ||
// Express initialisation | ||
... | ||
var auth = function (req, res, next) { | ||
if (req.headers.authorization === 'APIKey' || req.query.api_key === 'APIkey') { | ||
return next(); | ||
} | ||
res.status(401).send('Unauthorized'); | ||
}; | ||
app.use(stats({ auth: auth })); | ||
``` | ||
## Methods | ||
``` | ||
swagger: "2.0" | ||
@@ -70,3 +53,3 @@ info: | ||
title: your application | ||
host: example.com | ||
host: <%- hostname %> | ||
paths: | ||
@@ -73,0 +56,0 @@ /system/logs/{logType}: |
4369
47
94