-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (53 loc) · 1.7 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
// Import the modules we need
var express = require ('express')
var ejs = require('ejs')
var bodyParser= require ('body-parser')
var validator = require ('express-validator');
var session = require ('express-session');
// Create the express application object
const app = express()
const port = 8000
const mysql = require('mysql');
const expressSanitizer = require('express-sanitizer');
// Create an input sanitizer
app.use(expressSanitizer());
app.use(bodyParser.urlencoded({ extended: true }))
// Define the database connection
const db = mysql.createConnection ({
host: 'localhost',
user: 'appuser',
password: 'app2027',
database: 'StockTrack'
});
// Connect to the database
db.connect((err) => {
if (err) {
throw err;}
console.log('Connected to database');
});
global.db = db;
// Create a session
app.use(session({
secret: 'somerandomstuff',
resave: false,
saveUninitialized: false,
cookie: {
expires: 600000
}
}));
// Set up css
app.use(express.static(__dirname + '/public'));
// Set the directory where Express will pick up HTML files
// __dirname will get the current directory
app.set('views', __dirname + '/views');
// Tell Express that we want to use EJS as the templating engine
app.set('view engine', 'ejs');
// Tells Express how we should process html files
// We want to use EJS's rendering engine
app.engine('html', ejs.renderFile);
// Define our data
var shopData = {shopName: "StockTrack"}
// Requires the main.js file inside the routes folder passing in the Express app and data as arguments. All the routes will go in this file
require("./routes/main")(app, shopData);
// Start the web app listening
app.listen(port, () => console.log(`Example app listening on port ${port}!`))