Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var http = require('http');
- var url = require('url');
- var ROOT_DIR = "html/";
- var qs = require('querystring');
- const MONGO_URL = "mongodb://localhost/mean";
- const MONGO_USERNAME = "matchMaker";
- const MONGO_PASSWORD = "p@ssw0rd";
- const mongoose = require('mongoose');
- mongoose.connect(MONGO_URL, {
- auth: {
- user: MONGO_USERNAME,
- password: MONGO_PASSWORD
- },
- useNewUrlParser: true
- });
- mongoose.set('useCreateIndex', true);
- const meanSchema = require('./mean_schema.js').meanSchema;
- const User = mongoose.model('User', meanSchema);
- mongoose.connection.once('open', function () {
- console.log("Open connection!");
- });
- const bcrypt = require('bcrypt');
- http.createServer(function (req, res) {
- var urlObj = url.parse(req.url, true, false);
- console.log(urlObj.pathname);
- if (req.method == "GET") {
- if (urlObj.pathname.slice(urlObj.pathname.indexOf(".") + 1) == "html") {
- fs.readFile(ROOT_DIR + urlObj.pathname, function (err,data) {
- sendFile(err, data, res);
- });
- } else {
- fs.readFile(urlObj.pathname.slice(1), function (err,data) {
- sendFile(err, data, res);
- });
- }
- } else if (req.method == "POST" && req.url === "/index.html") {
- var body = '';
- req.on('data', function (data) {
- body += data;
- // Too much POST data, kill the connection!
- // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
- if (body.length > 1e6) {
- req.connection.destroy();
- mongoose.disconnect();
- }
- });
- req.on('end', function () {
- var post = qs.parse(body);
- console.log(post.username);
- console.log(post.password);
- console.log(post.formValue); //trying to pass the hidden form value to determine if login or registration
- /*
- Need to check if password is empty, because an empty
- password will still get hashed, so the Schema validation
- will see a hashed password and pass the required check.
- */
- if (!post.password || post.password.length <= 0) {
- res.writeHead(404);
- res.end("Password is required!");
- return;
- }
- bcrypt.hash(post.password, 10, function(err, hash) {
- console.log(post.password + " is hashed to: " + hash);
- var newUser = new User({
- userName: post.username,
- password: hash
- });
- meanSchema.index({userName: 'post.username'});
- //check request url
- if(req.url === "/index.html"){ //have to change between index and personalProfile until I can read form namevalues to determine which input
- //check if user in database
- User.findOne({userName: post.username}, function(err, user) {
- //return 412 if user doesnt exist
- if(user==null){
- console.log("user doesnt exist");
- res.writeHead(412);
- res.end(JSON.stringify(err));
- }else{
- //if user exists compare passwords
- bcrypt.compare(post.password, user.password, function (err, doc) {
- if (doc == true) {
- console.log("correct password");
- res.writeHead(302);
- res.end(JSON.stringify(doc));
- } else {
- console.log("incorrect password");
- res.writeHead(412);
- res.end(JSON.stringify(err));
- }
- });
- }
- });
- }
- if(req.url === "/personalProfile.html") { //have to change between index and personalProfile until I can read form namevalues to determine which input
- newUser.save({}, function (err, doc) {
- if (err) {
- console.log(err);
- res.writeHead(412);
- res.end(JSON.stringify(err));
- } else {
- console.log("\nSaved document: " + doc);
- res.writeHead(200);
- res.end(JSON.stringify(doc));
- }
- // mongoose.disconnect();
- });
- }
- });
- });
- } else if (req.method == "POST" && req.url === "/personalProfile.html") {
- var body = '';
- req.on('data', function (data) {
- body += data;
- // Too much POST data, kill the connection!
- // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
- if (body.length > 1e6) {
- req.connection.destroy();
- mongoose.disconnect();
- }
- });
- req.on('end', function () {
- var post = qs.parse(body);
- console.log(post.firstName);
- console.log(post.lastName);
- console.log(post.interests);
- var newUser = new User({
- firstName: post.firstName,
- lastName: post.lastName,
- interests: post.interests,
- });
- User.findOneAndUpdate({userName: post.username}, function (err, doc) {
- if (err) {
- console.log(err);
- res.writeHead(412);
- res.end(JSON.stringify(err));
- } else {
- console.log("\nSaved document: " + doc);
- res.writeHead(200);
- res.end(JSON.stringify(doc));
- }
- });
- })
- }
- }).listen(8080);
- function sendFile(err, data, res) {
- if (err) {
- res.writeHead(404);
- res.end(JSON.stringify(err));
- return;
- }
- res.writeHead(200);
- res.end(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement