Advertisement
yassmin

Untitled

May 12th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. var express = require('express');
  2. var app = express();
  3. var fs = require("fs");
  4. var bodyParser = require('body-parser');
  5. // Create application/x-www-form-urlencoded parser
  6. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  7. var authenticatedUser = null;
  8. app.use(express.static('public'));
  9. /**** This part to handle default request url= localhost:8081 ******/
  10. app.get('/', function (req, res) {
  11. res.sendFile( __dirname + "/" + "login.html" );
  12. })
  13. /**** This is an example on how to include local libraries, the libraries are defined in the html using
  14. script src="http://127.0.0.1:8081/test.js" so you need to create ar response for this get request ******/
  15. app.get('/test.js', function (req, res) {
  16. res.sendFile( __dirname + "/" + "test.js" );
  17. })
  18. /**** This is an example on login request where you need to verify the username and password for authentication
  19. normally login requests are POST method to prevent the password from showing on the url ****/
  20. app.post('/login', urlencodedParser, function (req, res) {
  21. // Get the values of the input text named username & password
  22. username = req.body.username;
  23. password = req.body.password;
  24. // console.log(username);
  25. // console.log(password);
  26. // Read JSON file containing the users to verify that the user is already registered and have access
  27. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
  28. // Note that err here is for handling any error occuring in opening the file
  29. data = JSON.parse( data );
  30. var flag = 0;
  31. for (var user in data) {
  32. if(username == data[user].name){flag = 1; authenticatedUser = user; break;}
  33. else{flag = 0; }
  34. }
  35. if(flag == 1){
  36. // Handling verified users by opening their html from the json file.
  37. fs.readFile( __dirname + "/" + "tables.json", 'utf8', function (err, tables_data) {
  38. tables_data = JSON.parse(tables_data);
  39. //console.log(tables_data[user].html);
  40. // necessary in case multiple clicks are commited
  41. res.setHeader('X-XSS-Protection', 0);
  42. res.writeHead(200, {'Content-Type': 'text/html'});
  43. // load the html of the user
  44. res.end((tables_data[user].html));
  45. });
  46.  
  47. }
  48. // Handle invalid login by redirecting the user to the login page once again
  49. else{res.sendFile( __dirname + "/" + "login.html" );}
  50. });
  51. })
  52. /******* Handle any update done by the user ********/
  53. app.post('/save', urlencodedParser, function (req, res) {
  54. // get the dummy input text field containing the entire html string
  55. body = req.body.dummy;
  56. // console.log(body);
  57. // open the JSON file to update with the new update user page after modifications
  58. fs.readFile( __dirname + "/" + "tables.json", 'utf8', function (err, data) {
  59. data = JSON.parse( data );
  60. data[authenticatedUser].html = "<html>" + body + "</html>";
  61. // Write the changes to the JSON file
  62. fs.writeFile(__dirname + "/" + "tables.json",JSON.stringify(data), function (err) {
  63. if (err) return console.log(err);
  64. //console.log(JSON.stringify(data));
  65. });
  66. console.log(authenticatedUser);
  67. res.setHeader('X-XSS-Protection', 0);
  68. res.writeHead(200, {'Content-Type': 'text/html'});
  69. res.end(data[authenticatedUser].html);
  70. });
  71.  
  72. })
  73.  
  74. var server = app.listen(8081, function () {
  75. var host = server.address().address
  76. var port = server.address().port
  77. console.log("Example app listening at http://%s:%s", host, port) })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement