Question: HTTP POST requests for http://localhost:3000/login. The middleware should parse the body of the HTTP POST request and extract the username and password carried in request

HTTP POST requests for http://localhost:3000/login. The middleware should parse the body of the HTTP POST request and extract the username and password carried in request body (Hint: use bodyParser.json()). Then it checks whether the username and password match any record in the userList collection in the database. If no, send ?Login failure? in the body of the response message. If yes, create a session variable ?userId? and store this user?s _id in the variable, and update ?status? of the user in userList collection to ?Online?; send name and icon of the current user, name and _id of friends of the current user and the number of messages that the user has not loaded in the conversation with each friend as a JSON string in the body of the response message if database operations are successful and the error message if failure. Again, you should design the format of the JSON string.

Attached here is the code I have written and need help to finish this part:

router.post('/login', bodyParser.json(), function(req, res) { var db req.db; var user_list_collection = db.get("userList");

router.post('/login', bodyParser.json(), function(req, res) { var db req.db; var user_list_collection = db.get("userList"); }); user_list_collection.find({'username': req.body.username}, {}, function(error, login_user) { if(error === null) { var user_list=[]; if ((login_user.length>0) && (login_user [0].password=req.body.password)){ } } })

Step by Step Solution

3.41 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is an example of middleware in Nodejs that should accomplish the tasks you described const bodyParser requirebodyparser const mongodb requiremong... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!