Node.js and Express Form Entry View

Hey guys I am making a website from Node.js and Angular Form Entry View for new users that would be entered into a MySql database, I was wondering how you would recommend I encrypt the passwords via java script to be stored in the database and I am also wondering how I could allow the submission of a photo that would be stored as well for a user profile?

+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| FIRSTNAME | char(60) | NO | | NULL | |
| LASTNAME | char(60) | NO | | NULL | |
| EMAIL | char(244) | NO | | NULL | |
| USER | char(50) | NO | PRI | NULL | |
| PASSWORDS | text | NO | | NULL | |
| PICTURE | mediumtext | YES | | NULL | |
+-----------+------------+------+-----+---------+-------+

here is the route file being called

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('createUser', function(req, res, next) {
  var fname = request.body.fName;
  var lname = request.body.lName;
  var email = request.body.email;
  var user = request.body.userName;
  var pass = request.body.Password;
  var pass2 = request.body.confPassword;

});

module.exports = router;

For password storage you can use something like bcrypt hashing. You would return a session token after the user successfully logs in (use crypto::randomBytes or smthg).

For images you'd need something to handle multipart form data. I've had to use busboy for streaming files but I think you'd want something of a bit easier to work with. You'll want to store it on the filesystem instead of the database since you're using MySQL. If you'd be using PostgreSQL though it would be fine to store in the large object store.

1 Like

bcrypt is the proper method of hashing.

Look into passport.js It will handle sessions, authentication methods and whatnot,

EDIT: Also, please use the sequelize node module when interacting with mysql databases. It's designed to use promises properly.

There's also KnexJS which is pretty nice. I think BookshelfJS is built on top of it.

True. I should have elaborated more, the idea behind using a query builder like that is to make use of promises, which are to few and far between with people new to node. If you start learning node with promises, you'll be happier in the end.

Also would any of you know how I could render a different .jade file by clicking on a button? I tried sending post requests but nothing, I am not sure if I understand how the www , app.js, and the routes work together to do server side control?

Can you go into more detail?

When I type like localhost:5000 it automatically maps and renders the jade file and sends this to the user. By default if I type localhost:5000/users it brings me to a different page, I am using express so I am not sure what I need to do to do server side calls to like a controller for example?
@aghost7

This is the github:
https://github.com/sdrafahl/ScrapForCash

You could send a redirect to the home page here if the data is entered properly.

ps. Since your project is to use Angular, why not just render on the client side? And then the client would interact with the server via REST.

Well I cant even load that page yet because I am not sure how to call like a routes java script file to render the jade file?

@aghost7

When you call render it will render the given jade file with the local variables that you pass into it. For the newUserPage that you have, I think the issue might be the forward slash that is in the name (but I'm not sure).

I got rid of the forward slash but it did nothing different. I understood the render part but I am confused on how do I make a server call from the client side. I tried creating a button that does this.

function newUser(){
    $.get('newU');
}

And then it makes GET requests to the server and I see them on the console so I know that part is right. My question though is how do I make a function in Node.js that can receive this message and then execute the newU.js that renders the page. It is this step in the middle I do not know how to do.

I think the issue is that you're trying to send data through a GET request. It should be a POST request.

I changed it and it didnt work.

Actually, the name of your template is also called createUser, not newUser.

createUser is supposed to take in data from a form from the page I am trying to render. not the one that renders the jade file.

This is what I meant: https://github.com/AGhost-7/ScrapForCash/commit/3473a462401d957e30c8da5542c374be38e8027a

edit: Also got the redirect on success and error message working: https://github.com/AGhost-7/ScrapForCash/commit/9a6b41dbbbfbcd078b3be052dc9569c358dac9d0

Its giving me this error
Error: Cannot find module './routes/newU'

You have a require that's incorrect in app.js I'm guessing. Take a look at the stack trace, you should be able to find the line number.

well it says the error is here
'node ./bin/www'.

npm ERR! Linux 4.4.0-24-generic
npm ERR! argv "node" "/usr/local/bin/npm" "start"
npm ERR! node v0.10.25
npm ERR! npm v3.10.5
npm ERR! code ELIFECYCLE
npm ERR! [email protected] start: node ./bin/www
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the scrap package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs scrap
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls scrap
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! /home/shanedrafahl/.local/share/Trash/files/scrap/npm-debug.log

And I cloned the latest git that you had also