You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So far, we have only made GET requests to our server. A POST request can send data securely through the request body. In order to make POST requests, first we need to include the "body-parser" library from our node_modules (included with express). Add these lines after the app variable:
Let's write a function to handle a POST request made to the 'login' endpoint, as if a user was trying to log in:
app.post('/login',function(req,res){constusername=req.body.username;constpassword=req.body.password;constmockUsername="billyTheKid";constmockPassword="superSecret";if(username===mockUsername&&password===mockPassword){res.json({success: true,message: 'password and username match!',token: 'encrypted token goes here'})}else{res.json({success: false,message: 'password and username do not match'})}})
Your entire file should look like this now:
constexpress=require('express');constapp=express();constbodyParser=require('body-parser');app.use(bodyParser.json())constmockUserData=[{name:'Mark'},{name:'Jill'}]app.get('/users',function(req,res){res.json({success: true,message: 'successfully got users. Nice!',users: mockUserData})})// colons are used as variables that be viewed in the paramsapp.get('/users/:id',function(req,res){console.log(req.params.id)res.json({success: true,message: 'got one user',user: req.params.id})})app.post('/login',function(req,res){// Typically passwords are encrypted using something like bcrypt before sending to databaseconstusername=req.body.username;constpassword=req.body.password;// This should come from the databaseconstmockUsername="billyTheKid";constmockPassword="superSecret";if(username===mockUsername&&password===mockPassword){// In practice, use JSON web token sign method here to make an encrypted tokenres.json({success: true,message: 'password and username match!',token: 'encrypted token goes here'})}else{res.json({success: false,message: 'password and username do not match'})}})app.listen(8000,function(){console.log('server is listening')})
Notice how we used app.post this time instead of app.get. We also compared the values passed from the request body to see if they match our mock data (which would normally come from a database). If they match, it will send a JSON file with an additional value, where a token could be stored. However, if they don't match, it will return an error message (without the token).
💡 As a security precaution, you should never save passwords directly into your database. Use a tool like bcrypt to save a hashed version, which will be decoded at login.
So far, we have only made GET requests to our server. A POST request can send data securely through the request body. In order to make POST requests, first we need to include the "body-parser" library from our node_modules (included with express). Add these lines after the app variable:
Let's write a function to handle a POST request made to the 'login' endpoint, as if a user was trying to log in:
Your entire file should look like this now:
Time to commit our changes!
The text was updated successfully, but these errors were encountered: