Reputation: 780
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
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:
Any side note will help me a lot from experts like you guys :)
Thank you all!!
Upvotes: 2
Views: 2578
Reputation: 4360
You need to import and require body-parser
npm install body-parser
and
var bodyParser = require('body-parser');
...
app.use(bodyParser.json());
Upvotes: 1
Reputation: 488
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 .
Upvotes: 0
Reputation: 376
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.
Upvotes: 0