Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var app = express();
- var fs = require("fs");
- var bodyParser = require('body-parser');
- app.use(bodyParser.json());
- // Create application/x-www-form-urlencoded parser
- var urlencodedParser = bodyParser.urlencoded({ extended: false })
- var authenticatedUser = null;
- app.use(express.static('Front End'));
- var arrays;
- // Email validation funciton
- function validateEmail(email)
- {
- // using regular expression.
- if (email.length > 0)
- {
- var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
- return re.test(email);
- }
- else
- return false;
- }
- // Password validation function
- function validatePassword(pass)
- {
- if (pass.length < 5)
- return false;
- else
- return true;
- }
- // Username validation function
- function validateUsername(name)
- {
- if (name.length > 0)
- return /^[a-zA-Z]*$/.test(name);
- else
- return false;
- }
- // Number of users already registered
- var usersCount = 0;
- /**** This part to handle default request url= localhost:8081 ******/
- app.get('/', function (req, res) {
- res.sendFile( __dirname + "/" + "homepage.html" );
- })
- app.get('/homepage.html', function (req, res) {
- res.sendFile( __dirname + "/" + "homepage.html" );
- })
- app.post('/login', urlencodedParser, function (req, res) {
- // Get the values of the input text email & password
- email = req.body.email;
- password = req.body.password;
- console.log(email);
- console.log(password);
- // Read JSON file containing the users to verify that the user is already registered and have access
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
- // Note that err here is for handling any error occuring in opening the file
- if (err)
- {
- return console.error(err);
- }
- data = JSON.parse(data);
- var flag = 0;
- for (var user in data)
- {
- if(email == data[user].email)
- {
- flag = 1;
- authenticatedUser = user;
- break;
- }
- else
- {
- flag = 0;
- }
- }
- if(flag == 1)
- {
- if (password == data[authenticatedUser].password)
- {
- console.log("This is the tasks page");
- res.sendFile( __dirname + "/" + "ProjectPh1V1.html");
- }
- else
- {
- res.sendFile( __dirname + "/" + "homepage.html");
- }
- }
- // Handle invalid login by redirecting the user to the login page once again
- else {
- console.log("User Not found");
- res.sendFile( __dirname + "/" + "homepage.html");
- }
- })
- })
- app.post('/array', function(req, res) {
- console.log("harby's herbyes");
- var data = req.body;
- console.log(data);
- arrays = data;
- fs.writeFile(__dirname + "/" + "user.json", JSON.stringify(data), function (err) {
- if (err) return console.log(err);
- //console.log(JSON.stringify(data));
- });
- res.send('success');
- });
- app.post('/register', urlencodedParser, function(req, res)
- {
- username = req.body.username;
- email = req.body.email;
- password = req.body.password;
- console.log(username);
- console.log(email);
- console.log(password);
- var f = 0
- if (validateUsername(username))
- f++;
- if (validateEmail(email))
- f++;
- if(validatePassword(password))
- f++;
- if (f == 3)
- {
- newUser = {
- "password" : password,
- "username" : username,
- "email" : email
- }
- var flag = 0;
- // Make sure this is a unique email address
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
- if (err)
- return console.error(err);
- data = JSON.parse(data);
- usersCount = 0;
- flag = 0;
- console.log('Going to check if there are duplicates');
- for (var user in data)
- {
- if(email == data[user].email)
- {
- flag=1;
- break;
- }
- usersCount++;
- }
- if (flag == 0)
- {
- console.log("ok no duplicates");
- // Add user to the JSON file database and show the homepage again
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
- newUser = {
- "password" : password,
- "username" : username,
- "email" : email
- }
- console.log("read users file!");
- if (err)
- return console.error(err);
- data = JSON.parse(data); // Converting it to string.
- data[usersCount] = newUser;
- console.log(data);
- res.sendFile( __dirname + "/" + "homepage.html" );
- app.get('/homepage.html', function (req, res) {
- res.sendFile( __dirname + "/" + "homepage.html" );
- })
- fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
- if (err)
- return console.log(err);
- console.log(JSON.stringify(data));
- })
- });
- }
- else
- {
- console.log("Yes, duplicates");
- // Show error message and freeze modal
- res.sendFile( __dirname + "/" + "homepage.html");
- }
- })
- }
- else
- {
- // Show error message and freeze modal
- res.sendFile( __dirname + "/" + "error.html");
- }
- })
- app.get('/ProjectPh1V1.html', function(req, res){
- res.sendFile( __dirname + "/" + "ProjectPh1V1.html" );
- })
- var server = app.listen(8081, function () {
- var host = server.address().address
- var port = server.address().port
- console.log("App listening at http://%s:%s", host, port)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement