-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.ts
33 lines (25 loc) · 884 Bytes
/
server.ts
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
import { createContext, httpListener } from '@marblejs/core';
import chalk from 'chalk';
import { createServer } from 'http';
import { Config } from '../config';
type HttpListener = ReturnType<typeof httpListener>;
export namespace Server {
const { host, port } = Config.server;
const onListen = () => {
console.info(chalk.green('[server] running'), `@ http://${host}:${port}/`);
};
const onClose = () => {
console.info(chalk.green(`[server] stopped`));
};
const onError = (error: Error) => {
console.error(chalk.red('[server] failed'), error.message);
};
export const create = async (httpListener: HttpListener) => {
const httpListenerWithContext = httpListener.run(createContext());
createServer(httpListenerWithContext)
.listen(port)
.on('close', onClose)
.on('error', onError)
.on('listening', onListen);
};
}