Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: expect 200 status #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import request from 'supertest';
import app from './app';

test('/ping', function(done) {
test('/ping', function() {
return request(app)
.get('/ping')
.expect(200);
Expand Down
95 changes: 43 additions & 52 deletions lib/health.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,61 @@
var os = require('os'),
df = require('node-df'),
path = require('path');
path = require('path'),
pkgDir = require('pkg-dir');

function loadMainPackageJSON(attempts) {
attempts = attempts || 1;
if (attempts > 5) {
throw new Error('Can\'t resolve main package.json file');
}
var mainPath = attempts === 1 ? './' : Array(attempts).join("../");
try {
return require.main.require(mainPath + 'package.json');
} catch (e) {
return loadMainPackageJSON(attempts + 1);
}
}

var pjson = loadMainPackageJSON();
var DEFAULT_PATH = '/ping';

/**
* Get system informaton
* @param {Function} cb
*/
function info(cb) {
df(function (error, diskInfo) {
if (error) {
diskInfo = {msg: 'Error fetching disk info', error: error};
}
pkgDir(path.resolve(__dirname, '../')).then(function(x) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not sure about whether this works when you use express-ping as a module in a bigger project that contains its own package.json. Which package.json file would this code find? The one that belongs to express-ping or the one that belongs to the bigger project?

return require(path.resolve(x, 'package.json'));
}).then(function(pjson) {
df(function (error, diskInfo) {
if (error) {
diskInfo = {msg: 'Error fetching disk info', error: error};
}

var data = {
timestamp: Date.now(),
uptime: process.uptime(),
var data = {
timestamp: Date.now(),
uptime: process.uptime(),

application: {
name: pjson.name,
version: pjson.version,
pid: process.pid,
title: process.title,
argv: process.argv,
versions: process.versions,
node_env: process.env.NODE_ENV,
dependencies: pjson.dependencies
},
application: {
name: pjson.name,
version: pjson.version,
pid: process.pid,
title: process.title,
argv: process.argv,
versions: process.versions,
node_env: process.env.NODE_ENV,
dependencies: pjson.dependencies
},

resources: {
memory: process.memoryUsage(),
loadavg: os.loadavg(),
cpu: os.cpus(),
disk: diskInfo,
nics: os.networkInterfaces()
},
resources: {
memory: process.memoryUsage(),
loadavg: os.loadavg(),
cpu: os.cpus(),
disk: diskInfo,
nics: os.networkInterfaces()
},

system: {
arch: process.arch,
platform: process.platform,
type: os.type(),
release: os.release(),
hostname: os.hostname(),
uptime: os.uptime(),
cores: os.cpus().length,
memory: os.totalmem()
}
};
system: {
arch: process.arch,
platform: process.platform,
type: os.type(),
release: os.release(),
hostname: os.hostname(),
uptime: os.uptime(),
cores: os.cpus().length,
memory: os.totalmem()
}
};

cb(null, data);
});
cb(null, data);
});
}).catch(cb);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
],
"dependencies": {
"express": "~3.x || ~4.x",
"node-df": "^0.1.1"
"node-df": "^0.1.1",
"pkg-dir": "^2.0.0"
},
"devDependencies": {
"babel-jest": "^20.0.3",
Expand Down
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,12 @@ pinkie@^2.0.0:
version "2.0.4"
resolved "https://registry.yarnpkg.com/pinkie/-/pinkie-2.0.4.tgz#72556b80cfa0d48a974e80e77248e80ed4f7f870"

pkg-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b"
dependencies:
find-up "^2.1.0"

prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
Expand Down