-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (76 loc) · 3.15 KB
/
server.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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var path = require("node:path");
var session = require("express-session");
var db = require("./db");
var port = process.env.PORT || 4500;
var SQLiteStore = require("connect-sqlite3")(session)
var restrict = require("./middlewares/restrict");
// app-wide middlewares
app.use(
session({
store: new SQLiteStore,
secret: "RSTUVWXYZabcdefghijklmyz0123456789!@#$%^&*()_+[]{}|;:,.<>?",
resave: true,
saveUninitialized: false,
cookie: {
secure: false,
// maxAge: 7*24*60*60*1000, //1 week
maxAge: 60*60*1000, //1 min
},
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
//create database tables
db.serialize(function create() {
db.run(
"CREATE TABLE IF NOT EXISTS roles (id INTEGER PRIMARY KEY AUTOINCREMENT, role VARCHAR(50) NOT NUll)"
);
db.run(
"CREATE TABLE IF NOT EXISTS auth (id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(50) NOT NUll UNIQUE, password VARCHAR(50) NOT NULL, user_id INTEGER)"
);
db.run(
"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(50) NOT NUll, middle_name VARCHAR(50) NULL, last_name VARCHAR(50) NOT NULL, DOB DATE NOT NULL, photo TEXT NULL, role_id INT NOT NULL, voted VARCHAR(10) NULL)"
);
db.run(
"CREATE TABLE IF NOT EXISTS parties (id INTEGER PRIMARY KEY AUTOINCREMENT, party VARCHAR(50) NOT NUll, logo TEXT NULL)"
);
db.run(
"CREATE TABLE IF NOT EXISTS positions (id INTEGER PRIMARY KEY AUTOINCREMENT, position VARCHAR(50) NOT NUll)"
);
db.run(
"CREATE TABLE IF NOT EXISTS candidates (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(50) NOT NUll, middle_name VARCHAR(50) NULL, last_name VARCHAR(50), position_id INTEGER NOT NULL, party_id INTEGER NOT NULL, photo TEXT NOT NULL)"
);
db.run(
"CREATE TABLE IF NOT EXISTS votes (id INTEGER PRIMARY KEY AUTOINCREMENT, candidate_id INTEGER NOT NULL, vote INT NOT NULL)"
);
});
// require route handlers
var voterRouter = require("./routes/voter");
var partyRouter = require("./public/js/party");
var dashboardRouter = require("./routes/dashboard");
var voteRouter = require("./routes/vote");
var loginRouter = require("./routes/login");
var logoutRouter = require("./routes/logout");
var electionsRouter = require("./routes/elections");
var partiesRouter = require("./routes/parties");
var candidatesRouter = require("./routes/candidates");
var indexRouter = require("./routes/index");
// route handler middlewares
app.use("/", indexRouter);
app.use("/login", loginRouter);
app.use("/voters", voterRouter);
app.use("/dashboard", restrict, dashboardRouter);
app.use("/parties", restrict, partyRouter);
app.use("/logout", restrict, logoutRouter);
app.use("/votes", restrict, voteRouter);
app.use("/elections",restrict, electionsRouter);
app.use("/parties", restrict, partiesRouter);
app.use("/candidates", restrict, candidatesRouter);
// start server
app.listen(port, function lister() {
console.log(`App is listening at http//localhost:${port}`);
});