NodeJS Troubleshooting Endpoint

05-01-2021

There are times when you might need to know information about your running NodeJS server. Here is a simple way to create an endpoint to expose valuable system for your production NodeJS application. Being able to quickly access system information allows for quick troubleshooting and triaging of potential issues.

In this short post I’ll walk through creating a simple endpoint to access system information.

NodeJS has a built in os module that gives access to valuable information about the underlying operating system of your NodeJS server. This could include information about the operating system platform, server uptime, CPUs, etc. Depending on your use case this could reveal valuable information for maintenance and troubleshooting. Using the os.platform() method we will return platform information about the OS. There is a lot of other information available, take your pick.

Here is a simple NodeJS/ ExpressJS server with a single GET /maintenance endpoint.

NOTE: This endpoint MUST REQUIRE user authorization

1
2
3
4
5
6
7
8
9
10
11
12
13
const express = require('express');
const os = require('os');

const app = express();

// TODO: Add user authorization!!!
app.get('/maintenance', (req, res, next) => {
res.send(os.platform());
});

app.listen(3000, () => {
console.log('Server running on port: 3000')
});