Closed github-learning-lab[bot] closed 4 years ago
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.
With this last push, your repository should look like this.
Click here to learn how to test a POST route.
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!