-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (52 loc) · 1.45 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
//---- step : 1.1
const express = require("express")
const app = express()
const dotenv = require("dotenv")
const mongoose = require("mongoose")
//---- step : 3
const multer = require("multer")
const path = require("path")
//---- step : 2.1
const authRoute = require("./routes/auth")
const authUser = require("./routes/user")
const authPost = require("./routes/post")
const authCat = require("./routes/categories")
//---- step : 1
dotenv.config()
//---- step : 2.2
app.use(express.json())
//---- step : 2.3 last ma file crate garne time
app.use("/images", express.static(path.join(__dirname, "/images")))
//---- step : 1.3
mongoose
.connect(process.env.CONNECTION_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
/* useCreateIndex: true,
useFindAndModify: true,*/
})
.then(console.log("Connected to MongoDB"))
.catch((err) => console.log(err))
//---- step : 3
const storage = multer.diskStorage({
destination: (req, file, callb) => {
callb(null, "images")
},
filename: (req, file, callb) => {
//callb(null, "file.png")
callb(null, req.body.name)
},
})
const upload = multer({ storage: storage })
app.post("/upload", upload.single("file"), (req, res) => {
res.status(200).json("File has been uploaded")
})
//---- step : 2
app.use("/auth", authRoute)
app.use("/users", authUser)
app.use("/posts", authPost)
app.use("/category", authCat)
//---- step : 1.2
app.listen(port, '0.0.0.0', () => {
console.log("backend running...")
})