-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (60 loc) · 1.94 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const session = require('express-session');
dotenv.config();
const passport = require("passport");
const { loginCheck } = require("./auth/passport");
loginCheck(passport);
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const nocache = require('nocache');
const multer = require("multer");
// Mongo DB connection
const database = process.env.MONGOLAB_URI;
mongoose
.connect(database, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => console.log("Connected the database"))
.catch((err) => console.log(err));
app.set("view engine", "ejs");
app.use(logger('dev'));
app.use(cookieParser());
app.use(nocache());
app.use(express.json());
//BodyParsing
app.use(express.urlencoded({ extended: false }));
// session configurations
app.use(function (req, res, next) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate');
next();
});
app.use(
session({
secret: "key",
resave: false,
saveUninitialized: true,
cookie: { maxAge: 60 * 60 * 1000 } // 1 hour
})
);
// photo and other file upload using multer
const fileStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "public/img/product_img");
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(null, file.fieldname + "-" + uniqueSuffix + file.originalname);
},
});
app.use(multer({ storage: fileStorage }).array("imgUrl", 5));
// static folder
app.use("/public", express.static("public"));
app.use(passport.initialize());
app.use(passport.session());
//Routes
app.use("/", require("./routes/user"));
app.use("/", require("./routes/login"));
app.use("/", require("./routes/admin"));
const PORT = process.env.PORT || 7000;
app.listen(PORT, console.log("Server has started at port " + PORT));