Registration API to handle registration function
- Accept a POST request to path "/register"
- Expect to receive data of a JSON object containing the following information of a user
- Email,
- First Name
- Last Name
- Password
- Gender
- Date of Birth
- Zipcode
- Height
- Gender Preference
- Age Preference Min
- Age Preference Max
- Race (optional)
- Religion (optional)
- Securely hash the Password for storage
- Allow user to upload a profile picture via path "/uploadProfile"
- Enforce necessary data validation and proper errors should be returned
- Return a success message if all validations are passed
- Store information in a local database, preferably, MySQL Requirements
- Accept input and return output in JSON
Clone the git repository:
git clone git@github.com:quangthuyhoang/nodejsRegistration.git
or
git clone https://github.com/quangthuyhoang/nodejsRegistration.git
- NodeJS
- Express
- MySQL
Make sure you have the most update-to-date node to run the server and MySQL database. I personally use WAMP for SQL database but you can also use XAMP/LAMP depending on your machine. If you choose to download and install WAMP. I recommend following the instructions on installation and setup VERY closely at http://www.wampserver.com/en/. It may take a while to getting everything up and running.
- Install all dependencies. After installing all required systems, you will need to install the latest dependency to run the server.
npm install
- Download and install Postman to test the API. https://www.getpostman.com/
If you clone this repository, there's a more manual way to test. See testing section.
-
Create a database in your MySQL DB. I created 'sampleDB' database.
-
Create a table in your database. Name the table 'user'.
-
'user' table should have 13 columns that should match the incoming user object with the column type and character limit:
- email: Type(varchar) 100 character limit
- firstname: Type(varchar) 25 character limit
- lastname: Type(varchar) 25 character limit
- password: Type(varchar) 60 character limit
- gender: Type(varchar) 6 character limit
- date_of_birth: Type(varchar) 10 character limit
- zipcode: Type(varchar) 10 character limit
- height: Type(decimal) 10 character limit
- genderpref: Type(varchar) 6 character limit
- ageminpref: Type(int) 10 character limit
- agemaxpref: Type(int) 10 character limit
- race: Type(varchar) 25 character limit
- religion: Type(varchar) 50 character limit
You will probably have a 14th column for your unique keyID. Make sure to setup for autoincrement.
- Create a .env file It should be located in the root directory. Setup a few configurations to run the server. Copy and paste the following key-value into your .env file. Update values where necessary.
# SERVER VAR
PORT=3000
# SESSION VAR - used in the session/option.js file
SALTROUNDS=10
keyid=user_sid
sessionSecret=secretString_key_i_like_sashimi
# DATABASE VAR - used in the database/mysqlDB.js file
DEVHOST=localhost
DBUSER=root
DBPASSWORD=
DBNAME=sampleDB
For the DATABASE Var, the DBUSER and DBPASSWORD is whatever you use to login to your local or online MySQL database. DBNAME is the name of your database we just created in XAMP/LAMP/WAMP, etc.
If you've finished everything up to this point, start the server with:
yarn start
or
node start
You should see Server is connected to port: 3000 or whatever port number you setup in your .env file.
Notes: Backend test was done using POSTMAN and some manual testing through web browser.
If you have postman and your node server is up and running. Send all request to http://localhost:3000/register below:
Use the picture below as a guide to test this route
You should see it return a json file for success or failure results.
{"message":"Registration Success!"}
or
{
"errors": {
"param": "email",
"message": "Email already exist in database."
}
}
{
"errors": [
{
"param": "password",
"message": "Passwords must be at least 5 chars long, contain at least one upper case, one lower case, and one number"
},
{
"param": "height",
"message": "Height must be a number"
},
{
"param": "height",
"message": "Height must be between 1 to 5 characters long"
}
]
}
Example output errors:
{
"errors": {
"param": "email",
"message": "Email already exist in database."
}
}
{
"errors": [
{
"param": "email",
"message": "Must be a valid email"
},
{
"param": "ageminpref",
"message": "Max age preference must be greater than min age preference."
},
{
"param": "firstname",
"message": "name must be between 1 to 15 characters"
},
{
"param": "lastname",
"message": "name must be between 1 to 15 characters"
},
{
"param": "zipcode",
"message": "Zipcode must be between 5 to 10 characters long with format 77777 or 77777-0000"
},
]
}
For the upload profile picture via /uploadProfile post route. I tested manually. You can also use test user registration via this method since I included a very simple form input.
- Go to server.js file
- Go to INDEX ROUTE
- Comment out res.send(msg) and Uncomment console.log(msg) and res.render('index')
// res.send(msg)
console.log(msg)
res.render('index', {msg: msg}) //--> testing purporses only
- save server.js file and restart node server.
- Go to http://localhost:3000/ in your browser. I used chrome.
- Enter all required information or leave them out. You will receive the same json information as Postman.
- If all entered fields are correct. then you will be redirected back to index page with no errors. You can verify success in your terminal console via "successful registation" message.
- Click and upload any picture file. If you're not logged in you will be redirected with a message you need to log in.
- If you already logged in, then you should be redirected back to the index page without error.
- You can also verify if it worked, if you look in your directory folder. You should now have a new folder "uploads" with your a new file.
pending
- Quang-Thuy Hoang