Building an e-Commerce website with Node – Part 10 – Creating a route for testing Schema

We are going to create a new route for testing our newly created schema. For that, we are going to use a tool named Postman that is used for testing routes/URLs.

We need to install this tool. You should go to their website to download it. They have a native app for all major operating systems. Or, if you use Chrome browser, there is an extension in the Chrome web store (easier and faster but with many limitations).

Since I use Linux, I have downloaded postman in .tar.gz format, extract it and run the executable application. It prompts you for creating an account. Once you are done with that everything is set up. Now you can test a URL without even having a webpage.

First, in server.js file we have to require user.js file. It is done in the similar way you required a library. Just below var mongoose = require('mongoose'); type the following code.


var User = require('./models/user');

It is saying to go to the project folder, then in the models folder, then get from there the user file (user.js).

Now, we need to install bodyParser in order to read the data from postman. You can read more about bodyParser at https://www.npmjs.com/package/body-parser. Basically, it takes the body of your request and parse it to what you want your server to receive (in POST or PUT requests).

You can install it via the terminal using the command npm install body-parser --save. Once it is finished the installation you can see it in the package.json file.

Of course, the bodyParser has to be added to the middleware. You do that by typing the following code just under the app.use(morgan('dev')); middleware.


app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));

We have to require bodyParser in the server.js too. Just below the mongoose (at the top of the file add the following code:


var bodyParser = require('body-parser');

We have created a home route for testing. We don’t need it anymore. Let’s delete it! The code below has to be deleted from the server.js file from middleware serction.


app.get('/', function(req, res) {
  res.json("This is awsome!");
});

Before going any further, let’s go back to the postman app and set it up.

Change the method from GET to POST then on the right side type http://localhost:3000/create-user (the link to the URL).
Switch to Body Tab.
Select form-data (it should be selected by default).
Add three new keys: name (with the value of YourName), password (with the value of YourPassword), and email (with the value of YourEmail).

Postman for testing routes

Let’s get back to server.js and create the actual route.


app.post('/create-user', function(req, res, next) {
  var user = new User();

  user.profile.name = req.body.name;
  user.password = req.body.password;
  user.email = req.body.email;

  user.save(function(err) {
    if (err) return next(err);
    res.json('Successfully created a new user');
  });
});

app is var app which is express library.
post is the method.
'/create-user' is the URL
var user = new User(); creates new instance of the object User.
user.profile.name = req.body.name;, user.password = req.body.password; , and user.email = req.body.email; are taken from user.js file (UserSchema).
user.save saves in the database (and the function(err) validates and gives a respons in case of no error is found).

Let’s test everything using Postman. Just click on the send button!

Test routes with postman

A new user has been successfully created. If you switch to x-www-form-urlencoded (you add new three key fields) and press the send button, you will get the same response. Basically you create a new user.

Now, let’s go to our database and check what has happened there.

mongoDB created users with Node.js

We have the two users we have just created. There is nothing saved for the first user (Dan), like name and email. That is because we allowed only .json data format and urlencoded format. Take a look at app.use(bodyParser() in the middleware section of your server.js file.

Terminal message - success created user

The terminal shows you the same, 200 which means success!

Leave a Reply