-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver317.js
More file actions
56 lines (46 loc) · 1.46 KB
/
server317.js
File metadata and controls
56 lines (46 loc) · 1.46 KB
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
var message = 'CSC-317 startup template\n'
+ 'This template uses nodeJS, express, and express.static\n';
var port = 3000;
var path = require('path');
var express = require('express');
var app = express();
const fs = require('fs');
var StaticDirectory = path.join(__dirname, 'public');
app.use(express.urlencoded({extended: true}));
app.use(express.static(StaticDirectory));
// Set up a route for the home page
// bcrypt
const bcrypt = require('bcrypt');
function hashit(pwd) {
const hash = bcrypt.hash(pwd, 12);
return hash;
}
async function compareit(pwd, hash) {
return await bcrypt.compare(pwd, hash);
}
// MySQL add account
const connection = require('./database');
app.post('/addAccount', (req, res) => {
let usernametemp = req.body.username;
const username = usernametemp.trim();
const password = hashit(req.body.password);
// Check if input field is empty
if (!(username == "" || req.body.password == "")) {
password.then((result) => {
const query = `INSERT INTO accounts VALUES ('${username}', '${result}')`;
connection.query(query, (err, res) => {
if (err) throw err;
});
});
// Redirect to login page
setTimeout(() => {
res.redirect('./login.html');
}, 800);
} else {
res.send('Username cannot be empty!');
}
});
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}/`);
});
console.log(message);