-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (57 loc) · 2.06 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
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const userRoute = require("./Routes/userRoute");
const db = require("./db");
const app = express(); // add extra capability
require("dotenv").config();
const corsOptions = {
origin: ["https://soma-food-blog.vercel.app", "http://localhost:3000"], // Ganti dengan URL frontend Anda
credentials: true, // Mengizinkan pengiriman cookie melalui CORS
};
// api routes // middlewares
app.use(express.json());
// app.use(cors());
app.use(cors(corsOptions));
app.use("/api/users", userRoute);
// root route
app.get("/", (req, res) => {
res.send("Just about learning to do serverless on vercel.");
});
app.get("/home", (req, res) => {
res.send("Hey there, Visitor!")
})
const port = process.env.PORT || 5000;
const uri = process.env.ATLAS_URI || 5000;
app.listen(port, (req, res) => {
console.log(`server running on port: ${port}`);
});
mongoose
.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("MongoDB connection establised");
// Serverless function
// app.get("/api/users", async (req, res) => {
// // Call the getUsers function using the within wrapper
// await within(getUsers, res, 7000);
// });
// // Within function to handle timeouts and errors
// async function within(fn, res, duration) {
// const id = setTimeout(() => res.json({ message: "There was an error with the upstream service!" }), duration);
// try {
// let data = await fn();
// clearTimeout(id);
// res.json(data);
// } catch (e) {
// res.status(500).json({ message: e.message });
// }
// }
// // Function to get users from the database
// async function getUsers() {
// return await db.getUsers();
// }
})
.catch((error) => console.log("MongoDB Connection failed: ", error.message));