-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
79 lines (63 loc) · 2.08 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require("express");
const path = require("path");
const methodOverride = require("method-override");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const multer = require("multer");
const uuid = require("uuid/v4");
const app = express();
// Load routes
const galaries = require("./routes/galarie");
const galaries_api = require("./routes/galarie-api");
const api = require('./routes/api');
const user = require('./routes/user');
// DB Config
const db = require("./config/key");
// Map global promise - get rid of warning
mongoose.Promise = global.Promise;
// Connect to mongoose
mongoose
.connect(db.MONGO_URI, {
useMongoClient: true
})
.then(() => console.log(`MONGODB connected on ${db.MONGO_URI}`))
.catch(err => console.log(err));
// ejs Middleware
app.use(express.static("public"));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
//generate uuid
const storage = multer.diskStorage({
destination: path.join(__dirname, "public/img"),
filename: (req, file, cb, filename) => {
cb(null, uuid() + path.extname(file.originalname));
}
});
app.use(multer({ storage }).single("image"));
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Static folder
app.use(express.static(path.join(__dirname, "public")));
// Method override middleware
app.use(methodOverride("_method"));
//CORS
app.use(function(req,res,next){
if (process.env.NODE_ENV === 'production') {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
} else {
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
}
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-width, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE");
next();
})
// Used routes
app.use("/user/", user);
app.use("/", galaries);
app.use("/ei-gallery", galaries_api);
app.use('/api/', api);
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server started on port http://localhost:${port}`);
});