SHARE
TWEET

Untitled

a guest May 7th, 2016 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. var express = require('express');
  4. //var x = require('to.js');
  5. var app = express();
  6. var path = require('path');
  7.  
  8. var fs = require('fs');
  9. var bodyParser = require('body-parser');
  10. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  11.  
  12.  
  13. // viewed at http://localhost:8080
  14. //app.use('/cssFiles',express.static(__dirname + '/public'));
  15.  
  16. // in the foolowing line :
  17. // instead of (/) we will add the desired url for example : webproject.html'
  18.  
  19. app.use(express.static('public'));
  20.  
  21. app.get('/',function(req,res) {
  22.  
  23. res.sendFile(path.join(__dirname + "/" + "home.html"));
  24.  
  25. });
  26.  
  27.  
  28.  
  29.  
  30. app.post('/home/tasks',urlencodedParser, function(req, res) {
  31.  
  32. var email= req.body.email;
  33. var password = req.body.password;
  34. var name = req.body.username;
  35.  
  36.  
  37.  
  38. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err,
  39. data) {
  40. data = JSON.parse( data );
  41. for  (var i in data) {
  42.  
  43.     if (data[i].password == password) {
  44.         console.log ("found");
  45.         res.sendFile(path.join(__dirname + '/webproject.html'));
  46.  
  47.         break
  48.     }
  49.     else {
  50.         console.log("not found");
  51.          
  52.  
  53.  
  54.  
  55.     }
  56.  
  57. }
  58.  res.redirect(req.get('refer'));
  59.  
  60. });
  61.  
  62.  
  63.  
  64. //res.end();
  65.  
  66.  
  67. console.log(email);
  68. console.log (password);
  69. console.log(name);
  70. //res.end(password);
  71.  
  72.  
  73.  
  74.  
  75. });
  76.  
  77.  
  78. app.get('/home/register',function(req,res) {
  79.     res.sendFile(path.join(__dirname + "/" + "register.html"));
  80. });
  81.  
  82.  
  83. app.get('/home/signin',function(req,res) {
  84.     res.sendFile(path.join(__dirname + "/" + "sign in.html"));
  85. });
  86.  
  87.  
  88.  
  89. app.listen(5000);
  90. console.log("server is listening on port 5000");
  91.  
  92.  
  93.  
  94.  
  95. // trying to deal with json files
  96.  
  97. // display the list of the users
  98.  
  99. /*
  100. var express = require('express');
  101.  
  102. var app = express();
  103.  
  104. var fs = require("fs");
  105.  
  106. app.get('/listUsers', function (req, res) {
  107.  
  108. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err,
  109.  
  110. data) {
  111.  
  112. console.log( data );
  113.  
  114. res.end( data );
  115.  
  116. });
  117. })
  118. var server = app.listen(8081, function () {
  119.  
  120. var host = server.address().address
  121.  
  122. var port = server.address().port
  123.  
  124. console.log("Example app listening at http://%s:%s", host, port)
  125. })*/
  126.  
  127.  
  128.  
  129.  
  130.  
  131. // adding user
  132.  
  133. /*var express = require('express');
  134. var app = express();
  135. var fs = require("fs");
  136. var user = {
  137. "user4" : {
  138. "name" : "mohit",
  139. "password" : "password4",
  140. "profession" : "teacher",
  141. "id": 4
  142. }
  143. }
  144. app.get('/addUser', function (req, res) {
  145. // First read existing users.
  146. fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err,
  147. data) {
  148. data = JSON.parse( data );
  149. data["user4"] = user["user4"];
  150. data["user4"].password = "wtf";
  151. var x = data["user4"].password;
  152. console.log( data["user4"].password );
  153. res.end( JSON.stringify());
  154. });
  155. })
  156. var server = app.listen(5000, function () {
  157. var host = server.address().address
  158. var port = server.address().port
  159. console.log("Example app listening at http://%s:%s", host, port)
  160. })*/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top