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');
- // Create application/x-www-form-urlencoded parser
- var urlencodedParser = bodyParser.urlencoded({ extended: false })
- var authenticatedUser = null;
- app.use(express.static('public'));
- /**** This part to handle default request url= localhost:8081 ******/
- app.get('/', function (req, res) {
- res.sendFile( __dirname + "/" + "login.html" );
- })
- /**** This is an example on how to include local libraries, the libraries are defined in the html using
- script src="http://127.0.0.1:8081/test.js" so you need to create ar response for this get request ******/
- app.get('/test.js', function (req, res) {
- res.sendFile( __dirname + "/" + "test.js" );
- })
- /**** This is an example on login request where you need to verify the username and password for authentication
- normally login requests are POST method to prevent the password from showing on the url ****/
- app.post('/login', urlencodedParser, function (req, res) {
- // Get the values of the input text named username & password
- username = req.body.username;
- password = req.body.password;
- // console.log(username);
- // 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
- data = JSON.parse( data );
- var flag = 0;
- for (var user in data) {
- if(username == data[user].name){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);
- res.writeHead(200, {'Content-Type': 'text/html'});
- // load the html of the user
- res.end((tables_data[user].html));
- });
- }
- // Handle invalid login by redirecting the user to the login page once again
- else{res.sendFile( __dirname + "/" + "login.html" );}
- });
- })
- /******* Handle any update done by the user ********/
- app.post('/save', urlencodedParser, function (req, res) {
- // get the dummy input text field containing the entire html string
- body = req.body.dummy;
- // console.log(body);
- // open the JSON file to update with the new update user page after modifications
- fs.readFile( __dirname + "/" + "tables.json", 'utf8', function (err, data) {
- data = JSON.parse( data );
- data[authenticatedUser].html = "<html>" + body + "</html>";
- // Write the changes to the JSON file
- fs.writeFile(__dirname + "/" + "tables.json",JSON.stringify(data), function (err) {
- if (err) return console.log(err);
- //console.log(JSON.stringify(data));
- });
- console.log(authenticatedUser);
- res.setHeader('X-XSS-Protection', 0);
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.end(data[authenticatedUser].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