This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
75 lines (61 loc) · 1.93 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
const express = require("express")
const path = require("path")
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const handlebar = require("express-handlebars")
const validator = require("express-validator")
const flash = require("connect-flash")
const session = require("express-session")
const passport = require("passport")
const routes = require("./routes")
const public = express.static("public")
const db = require("mongodb")
const mongoose = require("mongoose")
const Product = require("./data/products")
// connect to DB and initialize product inventory
mongoose.connect("mongodb://localhost/eccomerce", {useMongoClient: true})
Product.initInventory();
const app = express()
app.set("views", path.join(__dirname, "views"))
app.engine("handlebars", handlebar({defaultLayout:"main"}))
app.set("view engine", "handlebars")
app.use("/static", public)
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express(path.join(__dirname, "public")))
app.use(session({
secret: "electronics",
saveUninitialized: true,
resave: true
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(validator({
errorFormatter: function (parameter, message, value) {
var namespace = parameter.split("."),
root = namespace.shift(),
formParameter = root
while(namespace.length) {
formParameter += "[" + namespace.shift() + "]"
}
return {
parameter: formParameter,
message: message,
value: value
}
}
}))
app.use(flash())
app.use(function (req, res, next) {
res.locals.success_message = req.flash("success_message")
res.locals.error_message = req.flash("error_message")
res.locals.error = req.flash("error")
res.locals.user = req.user || null
next()
})
app.use("/", routes)
app.listen(3000, () => {
console.log("We've now got a server!")
console.log("listening on: http://localhost:3000")
})