-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (28 loc) · 782 Bytes
/
Copy pathserver.js
File metadata and controls
34 lines (28 loc) · 782 Bytes
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
const express = require('express');
const moment = require('moment-timezone');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Welcome to the API');
});
app.get('/time', (req, res) => {
const currentTime = new Date().toISOString();
let adjustedTime;
if (req.query.timezone) {
if (moment.tz.zone(req.query.timezone)) {
adjustedTime = moment.utc().tz(req.query.timezone).format();
} else {
adjustedTime = null;
}
}
res.json({
currentTime,
...(adjustedTime !== null && { adjustedTime })
});
});
module.exports = app;
if (require.main === module) {
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
}