-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (61 loc) · 2.12 KB
/
index.js
File metadata and controls
77 lines (61 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
knex = require("./db/knex"),
emailValidator = require("deep-email-validator"),
NaijaStates = require('naija-state-local-government');
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.urlencoded({extended: true}));
async function isEmailValid(email) {
return emailValidator.validate(email)
}
let errors = []
let success = [];
// function changeFunc() {
// var selectBox = document.getElementById("selectBox");
// var selectedValue = selectBox.options[selectBox.selectedIndex].value;
// let lgas = NaijaStates.lgas(selectedValue).lgas;
// return lgas;
// }
app.get("/", function(req, res){
res.render("landing", {success, errors, NaijaStates});
errors = success = [];s
});
app.post("/", (req, res) =>{
errors = [];
var user = req.body.user;
async function isValid() {
const {valid} = await isEmailValid(user.email);
return valid;
}
if(!user.name || !user.company || !user.country ||
!user.state || !user.lga || !user.email ||
!user.number || !user.message || !user.capacity){
errors.push({message: "please enter all fields"})
res.redirect("/")
}else if(isValid){
errors.push({message: "please provide a valid email address"})
res.redirect("/")
}
else{
knex('user').where('email', user.email).first()
.then((email) => {
if(email){
errors.push({message: "Email already exist"});
res.redirect("/")
errors = [];
}else{
knex('user')
.insert(user)
.then( () =>{
success.push({message: "You have successfully submitted"})
res.redirect("/")
})
}
});
}
})
app.listen(3004, () => {
console.log("The processo-africa server has started");
});