-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
36 lines (26 loc) · 876 Bytes
/
index.js
File metadata and controls
36 lines (26 loc) · 876 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
35
36
const http = require('http');
const http2 = require('http2');
const https = require('https');
const { PORT, ENV } = require('./constants');
const protocols = {
http,
http2,
https,
};
const DEFAULT_PROTOCOL = ENV === 'local' ? 'http' : 'https';
const createServer = (requestListener, options, logger = console, protocol = DEFAULT_PROTOCOL) => {
const serverArgs = [options, requestListener];
const serverProtocol = protocols[protocol];
if (!serverProtocol) {
throw new Error(
`Unkown protocol '${protocol}' provided to createServer. Please use either 'http', 'http2', or 'https'`,
);
}
const server = serverProtocol.createServer(...serverArgs);
server.listen(PORT);
if (logger.log && typeof logger.log === 'function') {
logger.log(`Server is now running on port ${PORT}`);
}
return server;
};
module.exports = createServer;