Closed laiso closed 3 months ago
Here's how you can add a healthcheck endpoint to your src/index.js file:
src/index.js
const express = require('express'); // Import express const fetch = require('node-fetch'); const app = express(); // Create express app // Existing getStatusCode function function getStatusCode() { return fetch('https://google.com').then((res) => { console.log(res); return res.status; }); } // Healthcheck endpoint app.get('/healthcheck', async (req, res) => { try { const status = await getStatusCode(); // Check status and respond accordingly if (status >= 200 && status < 300) { res.status(200).send('OK'); } else { res.status(500).send('External service unavailable'); } } catch (error) { res.status(500).send('Error checking health'); } }); // ... rest of your application code app.listen(3000, () => console.log('Server listening on port 3000!'));
Explanation:
express
express()
/healthcheck
app.get
getStatusCode
200 OK
500 Internal Server Error
Additional Considerations:
Remember to install Express:
npm install express
With these changes, you will have a basic healthcheck endpoint available at /healthcheck that you can use to monitor the health of your application.
Adding a Healthcheck Endpoint to src/index.js
Here's how you can add a healthcheck endpoint to your
src/index.js
file:Explanation:
express
library to create a web server and define routes.express()
./healthcheck
usingapp.get
.getStatusCode
function to check the status of an external service (Google, in this case).200 OK
if the external service is available.500 Internal Server Error
if there's an issue with the external service or an error during the check.Additional Considerations:
Remember to install Express:
With these changes, you will have a basic healthcheck endpoint available at
/healthcheck
that you can use to monitor the health of your application.