-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
63 lines (52 loc) · 1.45 KB
/
Copy pathserver.js
File metadata and controls
63 lines (52 loc) · 1.45 KB
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
const express = require("express");
const cors = require("cors");
const routes = require("./routes");
const bodyParser = require("body-parser");
require("dotenv").config();
const mongoose = require("mongoose");
// App
console.log(process.env.PUDO_URL);
const app = express();
app.use(cors());
//mongoose
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested, Content-Type, Accept Authorization"
);
if (req.method === "OPTIONS") {
res.header(
"Access-Control-Allow-Methods",
"POST, PUT, PATCH, GET, DELETE"
);
return res.status(200).json({});
}
next();
});
mongoose.connect(process.env.MONGO_CONNECTION, (err) => {
if (err) throw err;
console.log("connected to MongoDB");
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", function () {
console.log("Connected successfully");
});
app.use(routes);
app.use(
bodyParser.json({
verify: (req, res, buf, encoding) => {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || "utf8");
}
},
})
);
// Set port
const port = process.env.PORT || "1337";
app.set("port", port);
app.use("/", routes);
// Server
app.listen(port, () => console.log(`Server running on localhost:${port}`));