-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (34 loc) · 1.15 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
const express = require("express");
const app = express();
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const authRoute = require("./routes/auth");
const userRoute = require("./routes/users");
const postRoute = require("./routes/posts");
const categoryRoute = require("./routes/categories");
const multer = require("multer");
dotenv.config();
app.use(express.json());
mongoose.set("strictQuery", false);
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)
}
})
// storing the image locally under images folder
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.listen("3000", () =>{
console.log("Backend is running.");
});