-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (75 loc) · 2.22 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const express = require('express');
const app = express();
const path = require('path');
const pg = require("pg");
const ejsMate = require('ejs-mate');
const ExpressError = require('./utils/ExpressError.js');
const session = require('express-session');
const flash = require('connect-flash');
const bodyParser = require('body-parser');
require('dotenv').config();
const { Workbook } = require('exceljs');
const { GoogleGenerativeAI } = require("@google/generative-ai");
const PDFDocument = require('pdfkit');
app.use(bodyParser.json()); // Add this line to parse JSON data
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(express.static("public"));
app.engine("ejs", ejsMate);
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname,"/public")));
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: process.env.DBNAME,
password: process.env.DBPASS,
port: 5432,
});
db.connect();
// Set view engine
app.set('view engine', 'ejs');
const employee = require("./routes/employee.js");
const admin = require("./routes/admin.js");
const login = require("./routes/login.js");
// for cookie
app.use(session({
secret: "Its sgp",
resave: false,
saveUninitialized: true,
cookie : {
expires: Date.now() + 7 * 24 * 60 * 60 * 1000 ,// one week
maxAge : 7 * 24 * 60 * 60 * 1000 ,
httpOnly: true,
},
}));
// flash
app.use(flash());
// save for flash into locals
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.error = req.flash("error");
next();
});
// login
app.use("/", login);
// for employee
// we can make route of /emp
app.use("/emp", employee);
// for admin
// Admin route
app.use("/admin", admin);
// logout
app.get('/logout', (req, res) => {
res.redirect("/")
});
// if page is not available
app.all("*", (req, res, next) => {
console.log("into not found in index.js");
next(new ExpressError(404, "Page Not Found"));
});
app.use((err, req, res, next) => {
let { statusCode = 500, message = "Somthing went wrong" } = err;
res.status(statusCode).render("./pages/Error.ejs", { err });
});
app.listen(3000, () => {
console.log(`port no : 3000`);
});