-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (75 loc) · 2.77 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
82
83
const express = require("express");
const app = express();
// Body Parser.
const bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Helmet.
const helmet = require("helmet");
app.use(helmet({
crossOriginResourcePolicy: false,
}));
// CORS.
const cors = require("cors");
app.use(cors());
var allowlist =
[
"https://bitri-recipe.herokuapp.com",
"http://localhost:8000",
"https://bitri-recipe.web.app",
"https://bitrirecipeweb-production.up.railway.app",
"http://localhost:3000"
];
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (allowlist.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
};
// const corsOptionsDelegate = {
// origin: function (origin, callback) {
// if (allowlist.indexOf(origin) !== -1) {
// callback(null, true);
// } else {
// callback(new Error("Not allowaned by CORS"));
// }
// },
// };
// Static path from express.js local storage.
// app.use('/images', express.static('images'));
// Routes.
const usersRoutes = require("./routes/usersRoutes");
const recipesRoutes = require("./routes/recipesRoutes");
const commentsRoutes = require("./routes/commentsRoutes");
const likesRoutes = require("./routes/likesRoutes");
// const savesRoutes = require("./routes/savesRoutes");
app.use("/", cors(corsOptionsDelegate), usersRoutes);
app.use("/", cors(corsOptionsDelegate), recipesRoutes);
app.use("/", cors(corsOptionsDelegate), commentsRoutes);
app.use("/", cors(corsOptionsDelegate), likesRoutes);
// app.use("/", cors(corsOptionsDelegate), savesRoutes);
// const tesRoutes = require("./routes/tesRoutes");
// app.use("/", cors(corsOptionsDelegate), tesRoutes);
// app.use("/tes", (req, res) => {res.send("tes tanpa Cors, berhasil.")});
// app.use("*", (req, res) => {res.send("You access no one valid URL in this Site.")});
app.use("*", (req, res) => {res.send([
'Endpoints:',
'/users/all/?sort=[asc/desc]',
'/users/id/[id_user_number)]',
'/users/name/[name_user]',
'/recipes/all/?sort=[asc/desc]',
'/recipes/pagination/?limit=[limit_number]&pages=[page_number]&sort=[asc/desc]',
'/recipes/fivelikes',
'/recipes/id/[id_recipe_number)]',
'/recipes/name/?name=[name_recipe]&sort=[asc/desc/like]',
'/comments/all/?sort=[asc/desc]',
'/comments/new/?id_recipe=[id_number]&sort=[asc/desc]',
'/likes/all',
])});
// Listen the Port.
require('dotenv').config();
const port = process.env.PORT || 8000; // port database
app.listen(port, () => console.log(`[nodemon] running from port: '${port}'.`));