
- #Nodejs mongodb how to#
- #Nodejs mongodb install#
- #Nodejs mongodb code#
Enter a JSON object containing the username and password in the "Body" textarea:. In the URL field enter the address to the authenticate route of your local API. To authenticate a user with the api and get a JWT token follow these steps: Here's a screenshot of Postman after the request is sent and the new user has been registered: Click the "Send" button, you should receive a "200 OK" response with an empty JSON object in the response body. Enter a JSON object containing the required user properties in the "Body" textarea, e.g:. Select the "Body" tab below the URL field, change the body type radio button to "raw", and change the format dropdown selector to "JSON (application/json)". In the URL field enter the address to the register route of your local API. Change the http request method to "POST" with the dropdown selector on the left of the URL input field.
Open a new request tab by clicking the plus (+) button at the end of the tabs.To register a new user with the api follow these steps:
#Nodejs mongodb how to#
īelow are instructions on how to use Postman to register a new user with the api, authenticate a user to get a JWT token, and then make an authenticated request with the JWT token to retrieve a list of users from the api. Postman is a great tool for testing APIs, you can download it at. Follow the instructions below to test with Postman or hook up with one of the example single page applications available (React, Angular or Vue).
Start the api by running npm start from the command line in the project root folder, you should see the message Server listening on port 4000. #Nodejs mongodb install#
Install all required npm packages by running npm install from the command line in the project root folder (where the package.json is located).
#Nodejs mongodb code#
Download or clone the project source code from. Run MongoDB, instructions are available on the install page for each OS at. Install MongoDB Community Server from. - Updated Mongoose model and added instructions for testing with Postman and deploying to AWS.
- Updated to express-jwt version 6.0.0 to fix security vulnerability. Mongoose is used to connect to MongoDB, define the database schema and read/write data. The api is written in JavaScript for NodeJS and requires MongoDB to be running.
For an extended example that includes email verification, role based authorization and forgot password functionality see Node + Mongo - Boilerplate API with Email Sign Up, Verification, Authentication & Forgot Password.Īfter building a few front end tutorials and examples on user authentication with Angular and React I thought I'd put together a simple custom backend api that can be easily 'plugged in' to the front ends, or be easily hooked up with your own custom client application. In this tutorial we'll go through an example Node.js + MongoDB API that supports user registration, login with JWT authentication and user management.