forked from glitchgirl/brokensql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (97 loc) · 3.35 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//THIS FILE NEEDS TO BE FINISHED Y'ALL
var express = require('express');
var mysql = require('mysql');
var bodyParser = require("body-parser")
const PORT = process.env.PORT || 5000;
var app = express();
const { check, validationResult } = require("express-validator");
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(__dirname + "/public"));
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
database : 'brokensql',
password : 'root'
});
app.get("/", (req, res) => {
res.render("index");
});
app.post("/register",[
check("name")
//no empty space
.trim()
//name is required
.notEmpty().withMessage("name is required")
//if err go back
.bail()
//peramaters of symboles allowed
.matches(/^[^-']([a-zA-ZÀ-ÖØ-öø-ÿ '-](?!.*''|--| |- |' | '| -.*))+$/, 'g').withMessage("name should start with a letter and can only include letters with spaces, hyphens, apostrophies and the latin alphabet.")
//if err go back
.bail()
//checks the length of database column
.isLength({min:2, max:50}).withMessage("Please enter your full name 4 and 50 characters."),
check("username")
//letters and intergers are allowed
.trim()
//username must be inserted
.notEmpty().withMessage("username is required")
//if err go back
.bail()
//accepted characters
.matches(/^[^-']([a-zA-ZÀ-ÖØ-öø-ÿ0-9 '-](?!.*''|--| |- |' | '| -.*))+$/, 'g').withMessage("Username should start with a letter, and can only contain letters with spaces, numbers, hyphens, apostrophes and the latin alphabet.")
.bail()
.isLength({min:3, max:25}).withMessage("Please enter username, integers are allowed."),
check("email")
//there are no spaces in emails
.trim()
//email is required
.notEmpty().withMessage("Email is required")
//if err go back
.bail()
//excludes not allowed characters
.matches(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/,'g').withMessage("Email may contain letter, numbers, and must end with @domainname.com")
//if err go back
.bail()
//exceptable length of characters
.isLength({min:10, max:50}).withMessage("Please enter a email address up to 50 characters.")
],
(req, res) => {
// validation of results
let result = validationResult(req);
// puts them in an object
let errors = result.errors;
// consoles the errors
for (let key in errors) {
console.log("Validation failed:" + errors[key].msg);
}
let name = req.body.name;
let username = req.body.username;
let email = req.body.email;
if (!result.isEmpty()){
//if error, alert is shown
res.render("index", { errors, name, username, email })
}
else {
//if correct insert in to customer
let insert = "insert into customer(??, ??, ??) values (?, ?, ?)";
connection.query(insert, [ "name", "username", "email", name, username, email], (err,results)=> {
//if doesnt work go back
if (err) {
console.log(err);
}
let success = `Thank you!`
res.send(`thank you for registering ${name}!`)
});
}
});
app.get("/users", (req, res) => {
connection.query('select * from customer', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results);
res.send("results");
});
});
app.listen(PORT, () => {
console.log(`server running on ${PORT}`)
})