Advertisement
Guest User

edited

a guest
May 12th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. var http = require("http");
  2. var express = require("express");
  3. var app = express();
  4. app.use(express.static("stats"));
  5. app.use(function(req,res,next){
  6. res.header('Access-Control-Allow-Origin',"*");
  7. res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
  8. res.header('Access-Control-Allow-Headers','Content-Type');
  9. next();
  10. })
  11. var fs = require("fs");
  12. var id=-1;
  13. var bodyParser = require('body-parser');
  14. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  15.  
  16.  
  17. app.get('/', function (req, res) {
  18. res.sendFile( __dirname + "/" + "Home.html" );
  19.  
  20. })
  21. app.get('/signUp', function (req, res) {
  22. //console.log("here");
  23. res.sendFile( __dirname + "/" + "signUp.html" );
  24. })
  25. /** response for sign up submit button **/
  26. app.post('/list_Users', urlencodedParser, function (req, res) {
  27. // Prepare output in JSON format
  28. username = req.body.user;
  29. password = req.body.password;
  30. email= req.body.email;
  31.  
  32.  
  33. fs.readFile( __dirname + "/" + "employes.json", 'utf8', function (err, data) {
  34. data = JSON.parse( data );
  35. var flag=0;
  36. for (var i=0;i<data["id"].count;i++){
  37. if (email==data["users"][i].email){
  38. flag=1;
  39. break;
  40. }
  41. }
  42. if (flag==0){
  43. data['users'].push({"name":username,"password":password,"email":email,"id":data['id'].count, "data":""});
  44. id=data['id'].count;
  45. data['id'].count++;
  46. }
  47. else{
  48. // send error that email is already taken
  49. }
  50. fs.writeFile(__dirname + "/" + "employes.json",JSON.stringify(data), function (err) {
  51. if (err) return console.log("Error in opening employes json file");
  52. //console.log(JSON.stringify(data));
  53. });
  54. res.sendFile( __dirname + "/" + "project.html" );
  55. //res.end();
  56.  
  57.  
  58. });
  59.  
  60. })
  61. app.get('/onload', urlencodedParser, function (req, res) {
  62. // Prepare output in JSON format
  63.  
  64.  
  65. fs.readFile( __dirname + "/" + "employes.json", 'utf8', function (err, data) {
  66. var data = JSON.parse(data);
  67. var obj = data['users'][id].data;
  68.  
  69. var list= {task:JSON.parse(obj)};
  70. console.log(list);
  71. res.end(JSON.stringify(list));
  72.  
  73. });
  74.  
  75. })
  76. app.post('/tryjson', urlencodedParser, function (req, res) {
  77. // Prepare output in JSON format
  78. var obj=req.body.array;
  79. fs.readFile( __dirname + "/" + "employes.json", 'utf8', function (err, data) {
  80. data = JSON.parse(data);
  81. data['users'][id].data=obj;
  82. fs.writeFile(__dirname + "/" + "employes.json",JSON.stringify(data), function (err) {
  83. if (err) return console.log("Error in opening employes json file");
  84. //console.log(JSON.stringify(data));
  85. });
  86. });
  87. res.end();
  88. })
  89. var server = app.listen(8081, function () {
  90. var host = server.address().address
  91. var port = server.address().port
  92. console.log("Example app listening at http://%s:%s", host, port)
  93. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement