-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipInfo.js
39 lines (32 loc) · 1.39 KB
/
ipInfo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Import required libraries.
const express = require('express');
const logger = require('./utils/logger');
const config = require('./config');
const port = process.env.PORT || (config.port || 3000);
const { requestIp, getClientIp } = require('./services/ipService');
const rateLimiter = require('./middlewares/rateLimiter');
const timingMiddleware = require('./middlewares/timingMiddleware');
// Create an Express.js application.
const app = express();
app.use(requestIp.mw());
rateLimiter.configure(app);
app.use(timingMiddleware);
const environment = process.env.NODE_ENV || 'development';
// Disable TLS certificate validation in development mode
if (environment === "development")
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// Endpoint to retrieve the public IP address of the client.
app.get('/', async (req, res) => {
const ipAddress = await getClientIp(req);
res.send(ipAddress);
});
// Global error handler for logging and feedback
app.use((err, req, res, next) => {
logger.error(`${err.status || 500} - ${err.message} - ${req.originalUrl} - ${req.method}`);
res.status(500).send('An error occurred while processing your request. Please try again later.');
});
// Start the server
app.listen(port, () => {
logger.info(`Server is running in ${environment} mode and listening on port ${port}`);
console.log(`Server is running in ${environment} mode and listening on port ${port}`);
});