Express user restapi with MongoDb and JWT
|- node_modules
|- src
|- models
|- modules
|- routes
This folder contains the node modules.
This folder contains the application source code.
- modules : Codes with logic are found here.
- models : The codes where database operations are made are coded here.
- routes : Here are the codes that define the routing of incoming requests.
First install the nodejs and node modules
sudo apt-get install nodejs
npm install
Go to mongodb cloud website and create new user. Then open new project and cluster for free. Copy the mongodb connection string and paste the .env file to MONGO_URL section.
npm start
Below are all of the endpoints provided by the server, displayed by their relative URL, and the HTTP method with which you access them.
Register a user by sending the username, email and password to the server with the post method.
Example Request:
POST /api/user/ HTTP/1.1
Host: 192.168.1.2:8000
Content-Type: application/json
{
"username": "kado",
"email": "admin@company.com",
"password": "secret"
}
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"_id": "62425ba4c1186a0244754ab1",
"username": "furki",
"email": "furkan@kadorestapi.com",
"isAdmin": false,
"createdAt": "2022-03-29T01:06:44.527Z",
"updatedAt": "2022-03-29T01:06:44.527Z",
"__v": 0,
}
Log in by submitting the correct values and use other features by entering the returned token.
Example Request:
POST /api/v1/auth/login HTTP/1.1
Host: 192.168.1.2:8000
Content-Type: application/json
{
"username": "kado",
"password": "secret"
}
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"_id": "62425ba4c1186a0244754ab1",
"username": "furki",
"email": "furkan@kadorestapi.com",
"isAdmin": false,
"createdAt": "2022-03-29T01:06:44.527Z",
"updatedAt": "2022-03-29T01:06:44.527Z",
"__v": 0,
"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e..."
}
Example Request:
GET /api/v1/users HTTP/1.1
Host: 192.168.1.2:8000
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e..."
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json
[
{
"_id": "62425ba4c1186a0244754ab1",
"username": "furki",
"email": "furkan@kadorestapi.com",
"password": "U2FsdGVkX198x2nDXLvwnqyVf417dtBabckt5uV/bxc=",
"isAdmin": false,
"createdAt": "2022-03-29T01:06:44.527Z",
"updatedAt": "2022-03-29T01:06:44.527Z",
"__v": 0
},
{
"_id": "6241db42666b98065a2e8882",
"username": "kado",
"email": "admin@kadorestapi.com",
"password": "U2FsdGVkX19zCj7tZnPFCTPd6+8xDNWUXkf2sem6iM4=",
"isAdmin": true,
"createdAt": "2022-03-28T15:58:58.565Z",
"updatedAt": "2022-03-29T01:10:34.365Z",
"__v": 0
}
]
Example Request:
GET /api/v1/users/6241db42666b98065a2e8882 HTTP/1.1
Host: 192.168.1.2:8000
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e..."
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"_id": "62425ba4c1186a0244754ab1",
"username": "furki",
"email": "furkan@kadorestapi.com",
"isAdmin": false,
"createdAt": "2022-03-29T01:06:44.527Z",
"updatedAt": "2022-03-29T01:06:44.527Z",
"__v": 0,
}
Example Request:
DELETE /api/v1/users/6241db42666b98065a2e8882 HTTP/1.1
Host: 192.168.1.2:8000
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e..."
Example Response:
HTTP/1.1 200 OK
"User deleted successfully"
Example Request:
PUT /api/v1/users/6241db42666b98065a2e8882 HTTP/1.1
Host: 192.168.1.2:8000
token:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e..."
Content-Type: application/json
{
"username": "kadoman"
}
Example Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"_id": "62425ba4c1186a0244754ab1",
"username": "furki",
"email": "furkan@kadorestapi.com",
"isAdmin": false,
"createdAt": "2022-03-29T01:06:44.527Z",
"updatedAt": "2022-03-29T01:06:44.527Z",
"__v": 0,
}