File tree 5 files changed +104
-0
lines changed
5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ FROM node:18-alpine
2
+ WORKDIR /app
3
+ COPY package.json ./
4
+ RUN npm install
5
+ COPY . .
6
+ CMD ["node" , "server.js" ]
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " frontend" ,
3
+ "version" : " 1.0.0" ,
4
+ "dependencies" : {
5
+ "express" : " ^4.18.2" ,
6
+ "node-fetch" : " ^2.6.7"
7
+ }
8
+ }
Original file line number Diff line number Diff line change
1
+ let token = null ;
2
+
3
+ async function login ( ) {
4
+ const username = document . getElementById ( "username" ) . value ;
5
+ const password = document . getElementById ( "password" ) . value ;
6
+
7
+ const res = await fetch ( "/api/login" , {
8
+ method : "POST" ,
9
+ headers : { "Content-Type" : "application/json" } ,
10
+ body : JSON . stringify ( { username, password } ) ,
11
+ } ) ;
12
+
13
+ const data = await res . json ( ) ;
14
+ token = data . token ;
15
+ document . getElementById ( "output" ) . textContent = "Logged in!" ;
16
+ }
17
+
18
+ async function placeOrder ( ) {
19
+ const productId = document . getElementById ( "product" ) . value ;
20
+ const quantity = parseInt ( document . getElementById ( "quantity" ) . value ) ;
21
+
22
+ const res = await fetch ( "/api/orders" , {
23
+ method : "POST" ,
24
+ headers : {
25
+ "Content-Type" : "application/json" ,
26
+ "Authorization" : "Bearer " + token
27
+ } ,
28
+ body : JSON . stringify ( { productId, quantity } ) ,
29
+ } ) ;
30
+
31
+ const text = await res . text ( ) ;
32
+ document . getElementById ( "output" ) . textContent = text ;
33
+ }
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < title > Demo Shop</ title >
5
+ < script defer src ="app.js "> </ script >
6
+ </ head >
7
+ < body >
8
+ < h1 > Login</ h1 >
9
+ < input id ="username " placeholder ="Username " />
10
+ < input id ="password " type ="password " />
11
+ < button onclick ="login() "> Login</ button >
12
+
13
+ < h1 > Order</ h1 >
14
+ < input id ="product " placeholder ="Product ID " />
15
+ < input id ="quantity " type ="number " value ="1 " />
16
+ < button onclick ="placeOrder() "> Place Order</ button >
17
+
18
+ < pre id ="output "> </ pre >
19
+ </ body >
20
+ </ html >
Original file line number Diff line number Diff line change
1
+ const express = require ( "express" ) ;
2
+ const fetch = require ( "node-fetch" ) ;
3
+ const app = express ( ) ;
4
+
5
+ const AUTH_URL = process . env . AUTH_URL || "http://auth-python:8000" ;
6
+ const ORDERS_URL = process . env . ORDERS_URL || "http://orders-java:8080" ;
7
+
8
+ app . use ( express . static ( "public" ) ) ;
9
+ app . use ( express . json ( ) ) ;
10
+
11
+ app . post ( "/api/login" , async ( req , res ) => {
12
+ const response = await fetch ( `${ AUTH_URL } /auth/login` , {
13
+ method : "POST" ,
14
+ headers : { "Content-Type" : "application/json" } ,
15
+ body : JSON . stringify ( req . body ) ,
16
+ } ) ;
17
+
18
+ const data = await response . json ( ) ;
19
+ res . json ( data ) ;
20
+ } ) ;
21
+
22
+ app . post ( "/api/orders" , async ( req , res ) => {
23
+ const token = req . headers [ "authorization" ] ;
24
+ const response = await fetch ( `${ ORDERS_URL } /orders` , {
25
+ method : "POST" ,
26
+ headers : {
27
+ "Content-Type" : "application/json" ,
28
+ "Authorization" : token ,
29
+ } ,
30
+ body : JSON . stringify ( req . body ) ,
31
+ } ) ;
32
+
33
+ const text = await response . text ( ) ;
34
+ res . status ( response . status ) . send ( text ) ;
35
+ } ) ;
36
+
37
+ app . listen ( 3000 , ( ) => console . log ( "Frontend on :3000" ) ) ;
You can’t perform that action at this time.
0 commit comments