forked from sdswoc/cantcred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 2.06 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
const express = require("express"); //imports express
const app = express(); //makes app
require("dotenv").config(); // ensures .env is used in index.js
const port = process.env.PORT; //stores the port
const DB = process.env.DB;
const scr = process.env.SCR;
const mongoose = require("mongoose");
const session = require("express-session");
const cookieParser = require("cookie-parser");
const moment = require("moment");
var expressHbs = require("express-handlebars");
mongoose.set("strictQuery", true); //used this for depreciation warning
//connecting to database
mongoose
.connect(DB, { useNewUrlParser: true })
.then(() => console.log("Database connected"))
.catch((err) => console.log(err));
app.use(cookieParser());
const oneMonth = 1000 * 60 * 60 * 24 * 30;
app.use(
session({
secret: scr,
saveUninitialized: false,
cookie: { maxAge: oneMonth, httpOnly: true },
resave: false,
})
);
const routes = require("./routes/routes.js"); //imports routes
const userRoute = require("./routes/userRoute.js"); //imports user
const vendorRoute = require("./routes/vendorRoute.js"); //imports vendor routes
const hbs = require("hbs"); //imports handlebars
hbs.registerPartials(__dirname + "/views/partials", (err) => {}); // registers partials for handlebars
app.set("view engine", "hbs"); //sets view engine to handlebars
hbs.handlebars.registerHelper("formatDate", (date, format) => {
return moment(date).format(format);
});
hbs.handlebars.registerHelper("ifEquals", function (arg1, arg2, options) {
return arg1 == arg2 ? options.fn(this) : options.inverse(this);
});
app.use(express.json()); //sends data in form of json objects
app.use(express.urlencoded({ extended: false })); //body parser
app.use(express.static(__dirname + "/public")); //to include css files in hbs
app.use(express.static(__dirname));
app.use("/", routes); //calls routes
app.use("/users", userRoute); // calls user route
app.use("/vendor", vendorRoute); //calls venddor route
app.get("*", (req, res) => {
res.render("nopage");
});
//listens to port
app.listen(port, () => {
console.log(port);
});