-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
69 lines (59 loc) · 1.88 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
67
68
69
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const morgan = require("morgan");
const cors = require("cors");
const helmet = require("helmet");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
dotenv.config();
const port = process.env.PORT;
/* '''''''' db '''''''' */
// on success
mongoose.connect(process.env.MONGO_URI, { useUnifiedTopology: true }).then(() =>
console.log(`
DB Result:
----------------------------
Successfully Connected To DB!
----------------------------
`)
);
// on error
mongoose.connection.on("error", err =>
console.log(` Error While Connecting To DB: ${err.message} `)
);
/* ''''''' bringing routes ''''''''' */
const postRoutes = require("./routes/post");
/* ''''''' middlewares ''''''''' */
// creating middlewares
const myOwnMiddleWare = (req, res, next) => {
console.log("MiddleWare Applied!");
next();
};
// using own and 3rd party middlewares
app.use(morgan("common"));
app.use(cors());
var whitelist = ["http://127.0.0.1:4200"];
var corsOptionsDelegate = function(req, callback) {
var corsOptions;
if (whitelist.indexOf(req.header("Origin")) !== -1) {
corsOptions = { origin: true }; // reflect (enable) the requested origin in the CORS response
} else {
corsOptions = { origin: false }; // disable CORS for this request
}
callback(null, corsOptions); // callback expects two parameters: error and options
};
app.use(helmet());
app.use(helmet.hidePoweredBy({ setTo: "PHP 4.0.0" }));
app.use(bodyParser.json());
app.use("/", cors(corsOptionsDelegate), postRoutes);
app.use(myOwnMiddleWare);
app.listen(port, err => {
if (!err) {
console.log(`
******************************************************
NodeJs Server is listening on http://127.0.0.1:${port}
******************************************************
`);
}
});