-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (59 loc) · 1.87 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const express=require("express");
const app=express();
const Port=process.env.PORT ||8000;
const mongoose=require("mongoose");
const dotenv=require("dotenv").config();
const cors = require('cors')
app.use(cors())
const bodyParser=require("body-parser");
const path=require("path")
const multer=require("multer")
// const cors=require("cors");
// we need to allow the other host for using this server
// Add headers before the routes are defined
// for sending the json object
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
// for static folder access
app.use("/images",express.static(path.join(__dirname,"/images")))
// routes
// Multer implementation
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")
});
const authRoute=require('./routes/auth')
const userRoute=require('./routes/user');
const postRoute=require('./routes/post');
const categoryRoute=require('./routes/categories');
// DB connection
mongoose.set("strictQuery", false);
mongoose.connect("mongodb+srv://amit:amit@cluster0.c0w66ui.mongodb.net/?retryWrites=true&w=majority")
.then(console.log("Connected to mongoDB"))
.catch(err=>{
console.log("DB Connection Error",err);
});
// use routes
// this is for auth
app.use('/api/auth',authRoute);
// this is for user
app.use('/api/user',userRoute);
// this is for post
app.use('/api/posts',postRoute);
// this is for category
app.use('/api/categories',categoryRoute);
// Multer imple,ment
// app listen
app.listen(Port,(err)=>{
if(err){
console.log("Error In starting Server....");
}
console.log("backend server is running Up On Port:",`${Port}`);
})