-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·81 lines (65 loc) · 2.06 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import http from 'http'
import debug from 'debug'
import mongoose from 'mongoose'
import {MongoMemoryServer} from 'mongodb-memory-server'
import app from '../app.js'
// connect to the in-memory mongodb instance
const mongod = await MongoMemoryServer.create()
await mongoose.connect(mongod.getUri())
/**
* Normalize a port into a number, string, or false.
* @param {String} port - port to normalize
* @return {Boolean|Number|*} normalized port number on success or false on failure
*/
function normalizePort(port) {
const p = parseInt(port, 10);
// named pipe
if (Number.isNaN(p))
return port
// port number
if (p >= 0)
return p
return false
}
// Get port from environment and store in Express.
const port = normalizePort(process.env.PORT || '3000')
app.set('port', port);
// Create HTTP server
const server = http.createServer(app);
// Listen on provided port, on all network interfaces
server.listen(port)
// disconnect the DB when the server closes
server.on('close', async () => mongoose.disconnect())
// let the user know we're serving
server.on('listening', () => {
const addr = server.address();
const bind = typeof addr === 'string'
? `pipe ${addr}`
: `http://localhost:${addr.port}`;
debug('SRA:server')(`Listening on ${bind}`)
});
// disconnect from the DB on error
server.on('error', async (error) => {
await mongoose.disconnect()
if (error.syscall !== 'listen')
throw error;
const bind = typeof port === 'string'
? `Pipe ${port}`
: `Port ${port}`;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
/* eslint-disable-next-line no-console */
console.error(`${bind} requires elevated privileges`)
process.exit(1)
break;
case 'EADDRINUSE':
/* eslint-disable-next-line no-console */
console.error(`${bind} is already in use`)
process.exit(1)
break;
default:
throw error;
}
});
export default app