-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (54 loc) · 1.43 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require("dotenv").config();
const fs = require('fs');
const path = require('path');
const express = require("express");
const HttpError = require("./Models/HttpError");
const placesRoutes = require("./Routes/Places-Routes");
const userRoutes = require("./Routes/Users-Routes");
const mongoose = require("mongoose");
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json());
app.use('/uploads/images',express.static(path.join('uploads','images')));
app.use((req,res,next)=>{
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Headers','*');
res.setHeader('Access-Control-Allow-Methods','*');
next();
})
app.use("/api/places", placesRoutes);
app.use("/api/users", userRoutes);
app.use((req, res, next) => {
const error = new HttpError("Could not find the route", 404);
next(error);
});
app.use((error, req, res, next) => {
if(req.file){
fs.unlink(req.file.path,(err)=>{
if(err) console.log(err);
})
}
if (res.headerSent) {
next();
} else {
res.status(error.code || 500);
res.json({
message: error.message || "Unknown Error Occured",
});
}
});
mongoose
.connect(process.env.DATABASE_URL)
.then(() => {
app.listen(process.env.PORT, (error) => {
if(!error){
console.log('connected to database')
}else{
console.log(error);
}
});
})
.catch((error) => {
console.log(error);
});