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"); }); 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
Step: 1
Here is an example of middleware in Nodejs that should accomplish the tasks you described const bodyParser requirebodyparser const mongodb requiremong...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started