-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
88 lines (75 loc) · 2 KB
/
server.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
// IMPORTS
require("dotenv").config();
// required node modules
const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const multer = require("multer");
const passport = require("passport");
const pictureController = require("./controllers/controller_picture");
const routes = require("./routes/routes_index");
const userId = "5cf6bef51c9d440000db960c";
// EXPRESS
const app = express();
const port = process.env.PORT || 8080;
// PASSPORT
require("./config/passport")(passport);
// DATABASE
const dbConfig = require("./config/mongo");
mongoose.connect(dbConfig.mongoURI, {
useNewUrlParser: true
});
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database has started.");
});
db.on("reconnected", () => {
console.log("Database has reconnected.");
});
db.on("disconnected", () => {
console.log("Database has disconnected.");
});
// MULTER
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "./static/upload");
},
filename: function (req, file, cb) {
cb(null, userId + ".jpg");
}
});
const upload = multer({
storage: storage
});
// MIDDLEWARE
// EJS templating engine
app.set("view engine", "ejs");
// Body parser
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
// Session
app.use(session({
secret: "2C44-4D44-WppQ38S",
resave: true,
saveUninitialized: true
}));
// Passport
app.use(passport.initialize());
app.use(passport.session());
// Global application variables
app.use((req, res, next) => {
res.locals.user = req.user || null;
next();
});
// set root location for static files
app.use(express.static(__dirname + "/static"));
// ROUTING
app.post("/picture", upload.single("profilePicture"), pictureController.savePicture);
app.use("/", routes);
app.listen(port, () => {
console.log(`Server has started at localhost:${port}`);
});