-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
66 lines (53 loc) · 1.37 KB
/
app.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
if (process.env.NODE_ENV !== "production") {
require("dotenv/config");
}
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const mongoSanitize = require("express-mongo-sanitize");
const cors = require("cors");
const https = require("https");
const path = require("path");
const fs = require("fs");
// routers
const notesRouter = require("./routers/notesRouter");
const PORT = process.env.PORT;
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: false,
})
);
app.use(
mongoSanitize({
replaceWith: "_",
})
);
app.use(cors());
app.use(notesRouter);
mongoose.connect(process.env.db_connection, {});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", () => {
console.log("Database connected");
});
app.get("*", (req, res) => {
res.send("Page doesn't exist");
});
if (process.env.NODE_ENV !== "production") {
app.listen(PORT || 3000, () => {
console.log(`Serving on port ${PORT}`);
});
} else {
const sslServer = https.createServer(
{
key: fs.readFileSync(path.join(__dirname, "certs", "key.pem")),
cert: fs.readFileSync(path.join(__dirname, "certs", "cert.pem")),
},
app
);
sslServer.listen(PORT, () =>
console.log(`Secure Server running on port ${PORT}`)
);
}