Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express');
- var app = express();
- var bodyParser = require('body-parser');
- var fs = require("fs");
- // Create application/x-www-form-urlencoded parser
- var urlencodedParser = bodyParser.urlencoded({ extended: false })
- app.use(express.static('public'));
- app.use(bodyParser.json());
- var user;
- var id=0;
- var emails[];
- //This is function where the Interface is sent
- app.get('/', function (req, res)
- {
- res.sendFile( __dirname + "/" + "userinterface.html" );
- })
- app.post('/register', urlencodedParser, function (req, res) {
- // Prepare output in JSON format
- user =
- {
- "name":req.body.name,
- "email":req.body.email,
- "password":req.body.password
- };
- testEmail = req.body.email;
- console.log( testEmail);
- //Validation of E-mail
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
- {
- // Note that err here is for handling any error occuring in opening the file
- data = JSON.parse( data );
- var flag = 0;
- for (var user in data){
- if(testEmail == data[user].email)
- {
- flag=1;
- break;
- }
- }
- /*
- //front end...........
- function sendArrays() {
- var arrays = {
- 'inProgressArray[]': inProgress,
- 'completedArray[]': completed,
- 'archivedArray[]': archived
- }
- $.ajax({
- url: "http://127.0.0.1:8081/array",
- type: "POST",
- dataType: 'json',
- contentType: 'application/json',
- data: JSON.stringify(arrays)
- });
- }
- //BackEnd............
- app.post('/array', function(req, res) {
- var data = req.body;
- arrays = data;
- fs.writeFile(__dirname + "/database/data/" + "user" + thisUser + ".json",JSON.stringify(data), function (err) {
- if (err) return console.log(err);
- //console.log(JSON.stringify(data));
- });
- res.send('success');
- });
- */
- if(flag==0)
- {
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
- {
- user =
- {
- "name":req.body.name,
- "email":req.body.email,
- "password":req.body.password
- };
- data = JSON.parse( data );
- data[id] = user;
- // counter of users starts from 0.
- //We make array of saved e-mails with its corresponding ids...
- emails.push(req.body.email);
- id=id+1;
- res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
- fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err) {
- if (err)
- return console.log(err);
- console.log(JSON.stringify(data));
- });
- });
- } else {
- res.sendFile( __dirname + "/" + "Register.html" );
- }
- });
- })
- //BackEnd...........................................
- app.post('/array', function(req, res) {
- var data = req.body;
- //Creating JSON file for each user..
- console.log ("Server started and collections cleared.");
- //varialble id//
- fs.writeFile(__dirname + "/data/" + "user" + id + ".json",JSON.stringify(data), function (err) {
- if (err)
- return console.log(err);
- //console.log(JSON.stringify(data));
- });
- res.send('success');
- });
- app.post('/logout', function (req, res) {
- res.sendFile( __dirname + "/" + "userinterface.html" );
- })
- app.get('/register', function (req, res)
- {
- res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
- })
- app.post('/register', urlencodedParser, function (req, res) {
- fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data)
- {
- data = JSON.parse( data );
- data["i"] = user["i"];
- console.log( data );
- res.end( JSON.stringify(data));
- fs.writeFile(__dirname + "/" + "users.json",JSON.stringify(data), function (err)
- {
- if (err) return console.log(err);
- console.log(JSON.stringify(data));
- });
- });
- })
- app.post('/login', urlencodedParser, function (req, res)
- {
- // Get the values of the input text named username & password
- email = req.body.email1;
- password = req.body.password1;
- console.log(email);
- // 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
- data = JSON.parse( data );
- var flag = 0;
- for (var user in data) {
- if(email == data[user].email && password==data[user].password){flag = 1; authenticatedUser = user; break;}
- else{flag = 0; }
- }
- if(flag == 1)
- {
- // Handling verified users by opening their html from the json file.
- //fs.readFile( __dirname + "/" + "tables.json", 'utf8', function (err, tables_data) {
- //tables_data = JSON.parse(tables_data);
- //console.log(tables_data[user].html);
- // necessary in case multiple clicks are commited
- //res.setHeader('X-XSS-Protection', 0);
- // load the html of the user
- res.sendFile( __dirname + "/" + "finalOfPhase1.html" );
- }
- // Handle invalid login by redirecting the user to the login page once again
- else{res.sendFile( __dirname + "/" + "login.html" );}
- });
- })
- var server = app.listen(8081, function () {
- var host = server.address().address
- var port = server.address().port
- console.log("Example app listening at http://%s:%s", host, port)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement