-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
40 lines (34 loc) · 1.01 KB
/
server.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
import { createServer as createHttpServer } from 'node:http';
export function startServer(config, app) {
const logger = config.createLogger('www');
const server = createHttpServer(app);
return new Promise((resolve, reject) => {
server.listen(config.port, config.host);
server.on('error', onServerError);
server.on('listening', onServerListening);
function onServerError(err) {
if (err.syscall !== 'listen') {
reject(err);
return;
}
// Handle specific listen errors with friendly messages.
switch (err.code) {
case 'EACCES': {
reject(new Error(`Port ${config.port} requires elevated privileges`));
break;
}
case 'EADDRINUSE': {
reject(new Error(`Port ${config.port} is already in use`));
break;
}
default: {
reject(err);
}
}
}
function onServerListening() {
logger.info(`Listening on ${config.host}:${config.port}`);
resolve();
}
});
}