-
Notifications
You must be signed in to change notification settings - Fork 0
/
devServer.js
47 lines (42 loc) · 1.43 KB
/
devServer.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
40
41
42
43
44
45
46
47
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const {once} = require('events');
const express = require('express');
const app = express()
const port = 3000
async function main() {
// Load environment variables
for (const fileName of ['.env', '.env.local']) {
try {
const readStream = fs.createReadStream(fileName);
await once(readStream, 'open');
const envFile = readline.createInterface({
input: readStream,
terminal: false
});
envFile.on('line', (line) => {
const match = /^([^=#]+)=([^#]+)(?:#.*)?/.exec(line);
if (match !== null) {
const [, name, value] = match;
process.env[name] = JSON.parse(value);
}
})
await once(envFile, 'close');
} catch {}
}
// Serve HTML, JS and CSS
app.use('/', express.static('public'));
// Load and serve serverless functions
const functions = fs.readdirSync('./api');
functions.forEach(fileName => {
const name = path.parse(fileName).name;
const controller = require(`./api/${fileName}`);
app.get(`/api/${name}`, controller);
})
// Start server
app.listen(port, () => {
console.log(`\nDevelopment server running at http://localhost:${port}`);
})
}
main().then().catch(console.log);