-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev-server.js
More file actions
72 lines (62 loc) · 2.4 KB
/
Copy pathdev-server.js
File metadata and controls
72 lines (62 loc) · 2.4 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// A tiny, dependency-free local server for previewing api/calendar.js without
// installing the Vercel CLI. Vercel's real runtime adds a few conveniences on
// top of plain Node (`req.query`, `res.status().send()`) — this file adds
// just enough of that shim to run the same handler locally.
//
// Usage:
// 1. cp .env.example .env (then fill in GH_TOKEN and USERNAMES)
// 2. node dev-server.js
// 3. open http://localhost:9000/api/calendar?user=YOUR_USERNAME
const http = require('http');
const { URL } = require('url');
const path = require('path');
const fs = require('fs');
function loadEnvFile() {
const envPath = path.join(__dirname, '.env');
if (!fs.existsSync(envPath)) return;
fs.readFileSync(envPath, 'utf8')
.split('\n')
.forEach((line) => {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) return;
const eq = trimmed.indexOf('=');
if (eq === -1) return;
const key = trimmed.slice(0, eq).trim();
const value = trimmed.slice(eq + 1).trim();
if (!(key in process.env)) process.env[key] = value;
});
}
loadEnvFile();
const calendarHandler = require('./api/calendar');
const PORT = process.env.PORT || 9000;
const server = http.createServer(async (req, res) => {
const parsedUrl = new URL(req.url, `http://localhost:${PORT}`);
// Shim the pieces of the Vercel request/response objects this project uses.
req.query = Object.fromEntries(parsedUrl.searchParams.entries());
res.status = function status(code) {
res.statusCode = code;
return res;
};
res.send = function send(body) {
res.end(body);
return res;
};
if (parsedUrl.pathname === '/api/calendar') {
try {
await calendarHandler(req, res);
} catch (err) {
res.status(500).send(`Internal error: ${err.message}`);
}
return;
}
res.status(404).send('Not found. Try /api/calendar?user=YOUR_USERNAME');
});
server.listen(PORT, () => {
console.log(`Dev server running: http://localhost:${PORT}/api/calendar?user=YOUR_USERNAME`);
if (!process.env.GH_TOKEN) {
console.warn('Warning: GH_TOKEN is not set. Copy .env.example to .env and fill it in.');
}
if (!process.env.USERNAMES) {
console.warn('Warning: USERNAMES is not set. Copy .env.example to .env and fill it in.');
}
});