-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
48 lines (41 loc) · 1 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import app from './src/app'
import http from 'http'
import * as dotenv from 'dotenv'
// Initialize dotenv so it reads our `.env`-file
dotenv.config()
// Read port to start server on from `.env`, otherwise default to port 3000
const PORT = process.env.PORT || 3000
/**
* Create HTTP server.
*/
const server = http.createServer(app)
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(PORT)
/**
* Event listener for HTTP server "error" event.
*/
server.on('error', (err: NodeJS.ErrnoException) => {
if (err.syscall !== 'listen') {
throw err;
}
switch (err.code) {
case 'EACCES':
console.error(`🦸🏻 Port ${PORT} requires elevated privileges`)
process.exit(1)
break
case 'EADDRINUSE':
console.error(`🛑 Port ${PORT} is already in use`)
process.exit(1)
break
default:
throw err
}
})
/**
* Event listener for HTTP server "listening" event.
*/
server.on('listening', () => {
console.log(`🧑🏻🍳 Yay, server started on http://localhost:${PORT}`)
})