-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
53 lines (41 loc) · 1.11 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
41
42
43
44
45
46
47
48
49
50
51
52
53
require("dotenv").config();
const http = require("http");
const logger = require("morgan");
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const port = 3000;
const app = express();
const server = http.createServer(app);
const { botListener } = require("./index");
const mongoURI = "mongodb://mongo:27017/bookmarkerbot";
mongoose.connect(mongoURI, { useNewUrlParser: true });
const onError = (error) => {
if (error.syscall !== "listen") {
throw error;
}
const bind = `Port ${port}`;
switch (error.code) {
case "EACCES":
console.log(`${bind} requires elevated privileges`);
process.exit(1);
break;
case "EADDRINUSE":
console.log(`${bind} is already in use`);
process.exit(1);
default:
throw error;
}
};
const onListening = () => {
const addr = server.address();
const bind = `port ${addr.port}`;
console.log(`Listening on ${bind}`);
};
server.listen(port);
server.on("error", onError);
server.on("listening", onListening);
app.use(logger("dev"));
app.use(express.json());
app.use(cors());
botListener();