-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (55 loc) · 1.83 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
const path = require("path");
const ejsLayouts = require("express-ejs-layouts");
const express = require("express");
const app = express();
const session = require("express-session")
const inventoryController = require("./controllers/inventoryController");
const PrismaClient = require("@prisma/client").PrismaClient
const prisma = new PrismaClient()
require('dotenv').config();
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(ejsLayouts);
app.use(session({
key: 'user_sid',
secret: 'jennie',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000
}
}));
//serializes inventory
app.use(async (req, res, next) => {
req.inventory = await prisma.inventory.findMany()
next()
})
// Debugging console logs
// app.use(async (req, res, next) => {
// console.log("Inventory Database is");
// console.log(req.inventory)
// console.log("Entire session object:");
// console.log(req.session);
// next();
// });
app.set("views", path.join(__dirname, "/views"))
app.set("view engine", "ejs");
// Routes start here
app.get('/' , inventoryController.list)
app.get('/login' , (req, res) => { res.render("login") })
app.get('/about', (req, res) => { res.render("about") })
app.get('/contact', (req, res) => { res.render("contact") })
app.get('/create', (req, res) => { res.render("create") })
app.post('/create', inventoryController.create)
app.get('/inventory', (req, res) => { res.redirect('/') })
app.post('/inventory', inventoryController.update)
app.delete('/inventory', inventoryController.delete)
run = app.listen(8000, function () {
console.log(
"Server running. Visit: http://localhost:8000 in your browser 🚀"
);
});
module.exports.app = app
module.exports.run = run
module.exports.prisma = prisma