|
| 1 | +var express = require('express'); |
| 2 | +var path = require('path'); |
| 3 | +var fs = require('fs'); |
| 4 | +var app = express(); |
| 5 | +var mongo = require('mongodb'); |
| 6 | +var new_db = "mongodb://localhost:27017/agfgf"; |
| 7 | +var bodyParser = require('body-parser'); |
| 8 | +var crypto = require('crypto'); |
| 9 | +//Creating the database |
| 10 | + |
| 11 | +app.get('/',function(req,res){ |
| 12 | + res.set({ |
| 13 | + 'Access-Control-Allow-Origin' : '*' |
| 14 | + }); |
| 15 | + return res.redirect('/public/index.html'); |
| 16 | +}).listen(3000); |
| 17 | + |
| 18 | +console.log("Server listening at : 3000"); |
| 19 | +app.use('/public', express.static(__dirname + '/public')); |
| 20 | +app.use( bodyParser.json() ); |
| 21 | +app.use(bodyParser.urlencoded({ // to support URL-encoded bodies |
| 22 | + extended: true |
| 23 | +})); |
| 24 | + |
| 25 | +var getHash = ( pass , phone ) => { |
| 26 | + |
| 27 | + var hmac = crypto.createHmac('sha512', phone); |
| 28 | + |
| 29 | + //passing the data to be hashed |
| 30 | + data = hmac.update(pass); |
| 31 | + //Creating the hmac in the required format |
| 32 | + gen_hmac= data.digest('hex'); |
| 33 | + //Printing the output on the console |
| 34 | + console.log("hmac : " + gen_hmac); |
| 35 | + return gen_hmac; |
| 36 | +} |
| 37 | + |
| 38 | +// Sign-up function starts here. . . |
| 39 | +app.post('/sign_up' ,function(req,res){ |
| 40 | + var name = req.body.name; |
| 41 | + var email= req.body.email; |
| 42 | + var pass = req.body.password; |
| 43 | + var phone = req.body.phone; |
| 44 | + var password = getHash( pass , phone ); |
| 45 | + |
| 46 | + |
| 47 | + var data = { |
| 48 | + "name":name, |
| 49 | + "email":email, |
| 50 | + "password": password, |
| 51 | + "phone" : phone |
| 52 | + } |
| 53 | + |
| 54 | + mongo.connect(new_db , function(error , db){ |
| 55 | + if (error){ |
| 56 | + throw error; |
| 57 | + } |
| 58 | + console.log("connected to database successfully"); |
| 59 | + //CREATING A COLLECTION IN MONGODB USING NODE.JS |
| 60 | + db.collection("details").insertOne(data, (err , collection) => { |
| 61 | + if(err) throw err; |
| 62 | + console.log("Record inserted successfully"); |
| 63 | + console.log(collection); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + console.log("DATA is " + JSON.stringify(data) ); |
| 68 | + res.set({ |
| 69 | + 'Access-Control-Allow-Origin' : '*' |
| 70 | + }); |
| 71 | + return res.redirect('/public/success.html'); |
| 72 | + |
| 73 | + /** |
| 74 | + var filePath = path.join(__dirname, 'public/success.html'); |
| 75 | + var stat = fs.statSync(filePath); |
| 76 | +
|
| 77 | + res.writeHead(200, { |
| 78 | + 'Content-Type': 'text/html', |
| 79 | + 'Content-Length': stat.size |
| 80 | + }); |
| 81 | +
|
| 82 | + var readStream = fs.createReadStream(filePath); |
| 83 | + // We replaced all the event handlers with a simple call to readStream.pipe() |
| 84 | + readStream.pipe(res); |
| 85 | + */ |
| 86 | + |
| 87 | +}); |
| 88 | + |
| 89 | + |
| 90 | + |
0 commit comments