Skip to content

Commit af659a7

Browse files
final commit
Signed-off-by: Mohit Kumar <mkay6123@gmail.com>
0 parents  commit af659a7

25 files changed

+4582
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.vscode
2+
.data
3+
node_modules
4+
dist/**/*
5+

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
FROM node:carbon
2+
3+
ENV NPM_CONFIG_LOGLEVEL info
4+
ENV NODE_ENV production
5+
6+
ADD dist /home/node/app/dist
7+
ADD package.json /home/node/app/package.json
8+
9+
ENV MONGODB_URI mongodb://db:27017
10+
ENV JWT_SECRET ashdfjhasdlkjfhalksdjhflak
11+
ENV SALT_SECRET 8
12+
13+
RUN chown -R node:node /home/node/app
14+
15+
USER node
16+
WORKDIR /home/node/app
17+
RUN npm install --save
18+
19+
EXPOSE 3000
20+
21+
#CMD node dist/server.js
22+
CMD npm run watch

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
2+
# Node Rest API + JWT in TypeScript
3+
4+
- This is a simple **Node Rest Api** written in **Typescript**.
5+
- Routes can be protected with **JWT tokens**.
6+
- Authentification with Passport.
7+
8+
# How it works
9+
10+
- The API dispatches requests with well structured **routes**.
11+
- Routes are using **controllers** for API implementations.
12+
- Controllers are using **models** for Mongo persistence.
13+
- Routes can be protected with **JWT authentification middelwares** :
14+
```typescript
15+
import { Router } from "express";
16+
import { ProductController } from "../controllers/productController";
17+
import { AuthController } from "../controllers/authController";
18+
19+
20+
export class ProductRoutes {
21+
22+
public router: Router;
23+
public productController: ProductController = new ProductController();
24+
public authController: AuthController = new AuthController();
25+
26+
constructor() {
27+
this.router = Router();
28+
this.routes();
29+
}
30+
31+
routes() {
32+
this.router.get("/", this.productController.getProducts);
33+
this.router.get("/:id", this.productController.getProduct);
34+
// The following routes are protected
35+
this.router.post("/", this.authController.authenticateJWT, this.productController.createProduct);
36+
this.router.put("/:id", this.authController.authenticateJWT, this.productController.updateProduct);
37+
this.router.delete("/:id", this.authController.authenticateJWT, this.productController.deleteProduct);
38+
}
39+
}
40+
```
41+
42+
- Install dependencies
43+
```
44+
cd rest-api-node-jwt-typescript
45+
npm install
46+
npm run build
47+
```
48+
49+
50+
# Getting started
51+
52+
53+
## Step1 : Register a user
54+
Send a POST request to `http://localhost:3000/api/user/register`
55+
with the following payload ** :
56+
```json
57+
{
58+
"username": "me",
59+
"password": "pass"
60+
}
61+
```
62+
You should get a JWT token in the response :
63+
```json
64+
{
65+
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im1lMiIsImlhdCI6MTU1MDU4MTA4NH0.WN5D-BFLypnuklvO3VFQ5ucDjBT68R2Yc-gj8AlkRAs"
66+
}
67+
```
68+
69+
## Step2 : Create a Product
70+
Send a POST request to `http://localhost:3000/api/products`
71+
with the following payload :
72+
```json
73+
{
74+
"productId": "13",
75+
"name": "Orange",
76+
"price": 5,
77+
"quantity": 6
78+
}
79+
```
80+
You should get an authorization **denied** !
81+
```json
82+
{
83+
"status": "error",
84+
"code": "unauthorized"
85+
}
86+
```
87+
Add the JWT token to the Authorization header :
88+
```http
89+
Content-Type: application/json
90+
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Im1lMiIsImlhdCI6MTU1MDU4MTA4NH0.WN5D-BFLypnuklvO3VFQ5ucDjBT68R2Yc-gj8AlkRAs
91+
```
92+
You should have created the product !!
93+
```json
94+
{
95+
"data": {
96+
"_id": "5c6c0845e3eb8302ffd168c0",
97+
"productId": "13",
98+
"name": "Orange",
99+
"price": 5,
100+
"quantity": 6,
101+
"__v": 0
102+
}
103+
}
104+
```
105+
## Step2 : Get a Product
106+
You can get the product with or without token because the Get route of Product router is not protected with the JWT authentification middelware.
107+
Send a GET request to `http://localhost:3000/api/products/13`
108+
109+
You should get :
110+
```json
111+
[
112+
{
113+
"_id": "5c6bfc97e3eb8302ffd168be",
114+
"productId": "13",
115+
"name": "Orange",
116+
"price": 5,
117+
"quantity": 6,
118+
"__v": 0
119+
}
120+
```

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3"
2+
services:
3+
db:
4+
container_name: db
5+
image: mongo
6+
ports:
7+
- "27017:27017"
8+
volumes:
9+
- ./.data:/data/db
10+
api:
11+
container_name: api
12+
build: .
13+
ports :
14+
- "3000:3000"
15+
volumes:
16+
- ./:/home/node/app

0 commit comments

Comments
 (0)