-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (40 loc) · 1.38 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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const authRoute = require("./roots/auth");
const userRoute = require("./roots/users");
const postRoute = require("./roots/posts");
const categoryRoute = require("./roots/categories");
const multer = require("multer");
const path = require("path");
dotenv.config();
app.use(express.json());
app.use("/images",express.static(path.join(__dirname,"/images")))
mongoose
.connect(process.env.MONGO_URL)
.then(console.log("connected to MongoDB"))
.catch((err) => console.log(err));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "images");
},
filename: (req, file, cb) => {
cb(null, req.body.name);
},
});
const upload = multer({ storage: storage });
app.post("/api/upload", upload.single("file"), (req, res) => {
res.status(200).json("File has been uploaded");
});
app.use("/api/auth",authRoute);
app.use("/api/users",userRoute);
app.use("/api/posts",postRoute);
app.use("/api/categories",categoryRoute);
app.use(express.static(path.join(__dirname, "/blogapp/build")));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/blogapp/build', 'index.html'));
});
app.listen(process.env.PORT || 5000, () => {
console.log("backend is running.");
});