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.
In Express, words with a colon in front of them in the url are treated as variables. You can access the value of each variable through req.params, like this:
Let's test it out!
Add the function above your
app.listen
function in your server.js file, restart the server, and paste this into your browser url: http://localhost:8000/users/markNow check the terminal for the console.log of req.params.id. You should see the name 'mark', since it is the value we passed in place of the 'id' variable. Since we are running this in the terminal, our console.log will appear there (instead of the browser).
After testing that it works, push your changes your GitHub repo: