-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (29 loc) · 845 Bytes
/
index.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
const express = require('express');
require('dotenv').config();
const apicache = require('apicache');
const app = express();
const PORT = process.env.PORT || 3000;
const API_KEY = process.env.API_KEY;
const apiKeyMiddleware = (req, res, next) => {
const apiKey = req.headers['api-key'];
if (apiKey && apiKey === API_KEY) {
next();
} else {
res.status(401).json({ error: 'Unauthorized' });
}
};
app.use(express.json());
const cache = apicache.middleware;
app.get('/', cache('1 minutes'), (req, res) => {
let total = 0;
for (let i = 1; i < 1000000000; i++) {
total += i;
}
return res.json(total);
});
app.get('/data', apiKeyMiddleware, (req, res) => {
res.json({ message: 'Secret data here' });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});