2
votes

I have just started learning node.js (Express) and I created a simple application that communicate with a very simple mongo database. I have a collection called 'Users' in a database called 'testDB'. I created my skeleton in my node.js application and I followed 'separation of concern' logic.

In my controller folder I have a subfolder called usersController. Inside that subfolder, there is 2 .js files, one is usersControllers.js and the other is usersRoutes.js

Inside usersRoutes.js there is the following code:

    "use strict";
    var express = require('express');
    var router = express.Router();

    // require the controller here
    var usersController = require("./usersController.js")();


    router
        .get('/', usersController.getAllUsers)
        .post('/', usersController.createUser);

    module.exports = router;

As you can see, I am calling a function (factory) that is inside usersController.js called 'createUser'. This function is written like the following:

"use strict";

var MongoClient = require('mongodb').MongoClient;

var usersController = function(){
    var getAllUsers = function(req, res){
        MongoClient.connect('mongodb://localhost/testDB', function(err, db){
            if(err){
                throw err;
            }
            db.collection('Users').find().toArray(function(err, doc){
                if(err){
                    throw err;
                }
                else{
                    return res.status(200).json(doc);
                    db.close();
                }
            });

        });
    };

    var createUser = function (req, res) {
        MongoClient.connect('mongodb://localhost/testDB', function(err, db){

            console.log(req.body);
            db.close();
        });
    };

    return {
        getAllUsers: getAllUsers,
        createUser: createUser
    };
};
module.exports = usersController;

I have created a post man request to explore how to extract the body data that I am sending. The request is like the following

In the header I have 2 keys

  • Accept: application/json;charset=UTF-8
  • Content-Type: application/json

In the body I have the following raw text:

{
    "Users": {
        "First Name": "Ahmed",
        "Last Name": "Rahim",
        "Username": "rahima1",
        "Passwoed": "secure"
    }
}

Based on the previous scenario, I have a few questions:

  1. How to extract the body from the request. I tried to dig into 'req' but I couldn't find what I am looking for?
  2. passing a plain password like that is not good, right? Any suggestions to pass an encrypted password (maybe sha)?
  3. Is there something wrong in the request itself?

Any side note will help me a lot from experts like you guys :)

Thank you all!!

3
First, it's better to make one question per time. Second, what's exactly the problem?BrTkCa
I am trying to send a postman request (POST) and then in node I am trying to get the body request that I sentRiad
1. You need a bodyparser to get the data. 2. It's okay to send the password as plain text.adeneo
Ok.. I will take a look at the bodyparser thing.. is it something I require (is it a module)? how to include it ?Riad
Yes, it's a module, and no, it's not required, you could write your own code, but if you want the data automagically populated in req.body it's required.adeneo

3 Answers

0
votes

How to extract the body from the request. I tried to dig into 'req' but I couldn't find what I am looking for?

This should be done with a simple req.body. From the express docs on req.body: "Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer."

passing a plain password like that is not good, right? Any suggestions to pass an encrypted password (maybe sha)?

This answer sums it up pretty well. If you want to reliably encrypt password submission, you need to use https.

0
votes

I read the comments etc, and realize you haven't found a solution yet.

I can't really seem to find your actual mistake, but would like to provide some code example, that could possibly help you find it yourself.

Btw in your sent object, I wouldn't write "Users" with capital "U", but just "users".

So lets say we have this router file, that could be your createUser. It should look something like this:

router.post('createUser', function (req, res) {
  var users = req.body.users;
  res.json(users);
});

This should sent just what you have sent :-) Try and do that before you start talking with a database. That way you will can easily confirm that what you send is what you the server receives. Hope it is any help, else I am happy to assist you, if you have further questions .

0
votes

You need to import and require body-parser

npm install body-parser

and

var bodyParser = require('body-parser');
...
app.use(bodyParser.json());