teknet/index.js

53 lines
1.6 KiB
JavaScript

const express = require('express');
const app = express();
const port = process.env.SERVER_PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
//Logger
// [timestamp] method - url; UA; IP; roblox-id header if it exists
const timestamp = new Date().toISOString();
const method = req.method;
const url = req.originalUrl;
const userAgent = req.headers['user-agent'] || 'unknown';
const ip = req.headers['x-forwarded-for'] || req.remoteAddress || 'unknown';
const robloxId = req.headers['roblox-id'] || 'none';
console.log(`[${timestamp}] ${method} - ${url}; UA: ${userAgent}; IP: ${ip}; Roblox-ID: ${robloxId}`);
next();
});
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.get('/time.php', (req, res) => {
// This endpoint returns the current time in a specific format based on the timezone provided
const tz = req.query.zone || 'UTC';
const date = new Date();
try {
const options = { timeZone: tz, hour12: false };
const formatter = new Intl.DateTimeFormat('en-GB', {
...options,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const parts = formatter.formatToParts(date);
const get = type => parts.find(p => p.type === type).value;
const formatted = `${get('day')}${get('month')}${get('year')}${get('hour')}${get('minute')}${get('second')}`;
res.send(formatted);
} catch (e) {
res.status(400).send('Invalid timezone');
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});