-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
116 lines (106 loc) · 2.93 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const users = require("./routes/api/users");
const passport = require("passport");
const profile = require("./routes/api/profile");
const item = require("./routes/api/item");
const wishlist = require("./routes/api/wishlist");
const cart = require("./routes/api/cart");
// const multer = require("multer");
// const path = require("path");
// const crypto = require("crypto");
// const GridFsStorage = require("multer-gridfs-storage");
// const Grid = require("gridfs-stream");
// const methodOverride = require("method-override");
// init app
const app = express();
// Bring in cors
const cors = require("cors");
// using bodyParser middleware
app.use(
bodyParser.urlencoded({
extended: false
})
);
app.use(bodyParser.json());
// Use cors
app.use(cors());
app.use((request, response, next) => {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
response.header(
"Access-Control-Allow-Headers",
"Content-type, Accept, x-access-token, X-Key"
);
response.header("Access-Control-Allow-Credentials", true);
credentials: "same-origin";
if (request.method == "OPTIONS") {
response.status(200).end();
} else {
next();
}
});
// creating database connection
const db = require("./config/keys").mongoURI;
mongoose
.connect(
db,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
)
.then(res => {
console.log("Database connected successfully!");
})
.catch(err => {
console.log(err);
});
// Initialize gfs
// let gfs;
//
// mongoose.connection.once("open", () => {
// // Initialise gfs stream
// gfs = Grid(mongoose.connection.db, mongoose.mongo);
// gfs.collection("uploads");
// });
//
// // Create storage objects
// const storage = new GridFsStorage({
// url: db,
// file: (req, file) => {
// return new Promise((resolve, reject) => {
// crypto.randomBytes(16, (err, buf) => {
// if (err) {
// return reject(err);
// }
// const filename = buf.toString("hex") + path.extname(file.originalname);
// const fileInfo = {
// filename: filename,
// bucketName: "uploads"
// };
// resolve(fileInfo);
// });
// });
// }
// });
// const upload = multer({ storage });
// POST route /loads form
// app.post("/upload", upload.single("file"), (req, res) => {
// console.log({ file: req.file });
// });
// Passport middleware
app.use(passport.initialize());
require("./config/passport")(passport);
// Use Routing
app.use("/api/users", users);
app.use("/api/profile", profile);
app.use("/api/item", item);
app.use("/api/wishlist", wishlist);
app.use("/api/cart", cart);
// listening app on a specified port
const port = process.env.PORT || 5001;
app.listen(port, () => {
console.log(`App is running on port ${port}`);
});