-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (27 loc) · 861 Bytes
/
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
var express=require("express");
var mongoose=require("mongoose");
var bodyparser=require("body-parser");
var cors=require("cors");
var path=require("path");
var app=express();
const port=3000;
var route= require("./routes/route");
mongoose.connect("mongodb://localhost:27017/details");
mongoose.connection.on("connected",()=>{
console.log("Mongodb connected");
});
mongoose.connection.on("error",(err)=>{
if(err){
console.log("Mongodb connection failed!!:"+err);
}
});
app.use(bodyparser.json()); // HAS TO BE BEFORE ROUTE DECLARATION!!!!!!
app.use(cors());// THIS ALSO HAS TO BE BEFORE ROUTE !!!!!
app.use("/api",route);
app.use(express.static(path.join(__dirname,"public")));
app.get("/",function(req,res){
res.send("foobar");
});
app.listen(port,function(){
console.log("Server started at port:"+port);
});