
250630 박정하 API get 통신 시 server proxy 설정으로 인해 params 누락 되는 오류 수정
@4da0534e6ff1055a17d9ad2962d82bd6b39c81d1
+++ client/views/robots.txt
... | ... | @@ -0,0 +1,2 @@ |
1 | +User-agent: * | |
2 | +Disallow:/(파일 끝에 줄바꿈 문자 없음) |
--- server/modules/web/Server.js
+++ server/modules/web/Server.js
... | ... | @@ -18,8 +18,8 @@ |
18 | 18 |
const newLineStream = require("new-line"); |
19 | 19 |
|
20 | 20 |
webServer.use((req, res, next) => { |
21 |
- res.header("X-Frame-Options", "SAMEORIGIN"); // or 'SAMEORIGIN' or 'ALLOW-FROM uri' |
|
22 |
- next(); |
|
21 |
+ res.header("X-Frame-Options", "SAMEORIGIN"); // or 'SAMEORIGIN' or 'ALLOW-FROM uri' |
|
22 |
+ next(); |
|
23 | 23 |
}); |
24 | 24 |
|
25 | 25 |
/** |
... | ... | @@ -28,7 +28,7 @@ |
28 | 28 |
* @dscription : HTTP Server start |
29 | 29 |
*/ |
30 | 30 |
webServer.listen(PORT, function () { |
31 |
- Logger.logging(`★★★ Node.js를 활용한 Web Server 구동(Port:${PORT}) ★★★`); |
|
31 |
+ Logger.logging(`★★★ Node.js를 활용한 Web Server 구동(Port:${PORT}) ★★★`); |
|
32 | 32 |
}); |
33 | 33 |
|
34 | 34 |
/** |
... | ... | @@ -37,11 +37,21 @@ |
37 | 37 |
* @dscription : Intercepter 역할을 하는 미들웨어 기능 |
38 | 38 |
*/ |
39 | 39 |
webServer.use(function (request, response, next) { |
40 |
- let ip = request.headers["x-forwarded-for"] || request.socket.remoteAddress; |
|
41 |
- Logger.logging( |
|
42 |
- `[HTTP] ${request.url} (Method: ${request.method}, IP: ${ip})` |
|
43 |
- ); |
|
44 |
- next(); |
|
40 |
+ let ip = request.headers["x-forwarded-for"] || request.socket.remoteAddress; |
|
41 |
+ Logger.logging( |
|
42 |
+ `[HTTP] ${request.url} (Method: ${request.method}, IP: ${ip})` |
|
43 |
+ ); |
|
44 |
+ next(); |
|
45 |
+}); |
|
46 |
+ |
|
47 |
+/** |
|
48 |
+ * @author : 김성원 |
|
49 |
+ * @since : 2024.05.30 |
|
50 |
+ * @dscription : robots.txt |
|
51 |
+ */ |
|
52 |
+webServer.get("/robots.txt", function (request, response) { |
|
53 |
+ //response.sendFile을 통한 HTTP html reponse (html내용 Streaming) |
|
54 |
+ response.sendFile(`${BASE_DIR}/client/views/robots.txt`); |
|
45 | 55 |
}); |
46 | 56 |
|
47 | 57 |
/** |
... | ... | @@ -50,8 +60,8 @@ |
50 | 60 |
* @dscription : ROOT URL -> index.html |
51 | 61 |
*/ |
52 | 62 |
webServer.get("/", function (request, response) { |
53 |
- //response.sendFile을 통한 HTTP html reponse (html내용 Streaming) |
|
54 |
- response.sendFile(`${BASE_DIR}/client/views/index.html`); |
|
63 |
+ //response.sendFile을 통한 HTTP html reponse (html내용 Streaming) |
|
64 |
+ response.sendFile(`${BASE_DIR}/client/views/index.html`); |
|
55 | 65 |
}); |
56 | 66 |
|
57 | 67 |
/** |
... | ... | @@ -60,40 +70,45 @@ |
60 | 70 |
* @dscription : 화면요청 URL 처리 |
61 | 71 |
*/ |
62 | 72 |
webServer.get("*.page", function (request, response, next) { |
63 |
- //index.html 내용을 직접 Streaming하여 Response, Streaming 중간에 내용 수정 |
|
64 |
- //수정 내용 : URL 요청이 아닌, 브라우저에 표시된 URL만 변경하여, 해당하는 URL PATH의 Vue Component를 routing하기 위함 |
|
65 |
- const StreamTransform = new Transform(); |
|
66 |
- StreamTransform._transform = function (data, encoding, done) { |
|
67 |
- let fileContent = data.toString(); |
|
68 |
- let replaceBeforeContent = `<script id="app-start-vue-page">const APP_USER_HTTP_REQUEST_URL = '/';</script>`; |
|
69 |
- let replaceAfterContent = `<script id="app-start-vue-page">const APP_USER_HTTP_REQUEST_URL = '${request.params["0"]}.page';</script>`; |
|
70 |
- fileContent.replace(replaceBeforeContent, replaceAfterContent); |
|
71 |
- this.push(fileContent); |
|
72 |
- done(); |
|
73 |
- }; |
|
74 |
- //Streaming 진행 |
|
75 |
- FS.createReadStream(`${BASE_DIR}/client/views/index.html`) |
|
76 |
- .pipe(newLineStream()) |
|
77 |
- .pipe(StreamTransform) |
|
78 |
- .pipe(response); |
|
73 |
+ //index.html 내용을 직접 Streaming하여 Response, Streaming 중간에 내용 수정 |
|
74 |
+ //수정 내용 : URL 요청이 아닌, 브라우저에 표시된 URL만 변경하여, 해당하는 URL PATH의 Vue Component를 routing하기 위함 |
|
75 |
+ const StreamTransform = new Transform(); |
|
76 |
+ StreamTransform._transform = function (data, encoding, done) { |
|
77 |
+ let fileContent = data.toString(); |
|
78 |
+ let replaceBeforeContent = `<script id="app-start-vue-page">const APP_USER_HTTP_REQUEST_URL = '/';</script>`; |
|
79 |
+ let replaceAfterContent = `<script id="app-start-vue-page">const APP_USER_HTTP_REQUEST_URL = '${request.params["0"]}.page';</script>`; |
|
80 |
+ fileContent.replace(replaceBeforeContent, replaceAfterContent); |
|
81 |
+ this.push(fileContent); |
|
82 |
+ done(); |
|
83 |
+ }; |
|
84 |
+ //Streaming 진행 |
|
85 |
+ FS.createReadStream(`${BASE_DIR}/client/views/index.html`) |
|
86 |
+ .pipe(newLineStream()) |
|
87 |
+ .pipe(StreamTransform) |
|
88 |
+ .pipe(response); |
|
79 | 89 |
}); |
80 | 90 |
|
81 | 91 |
/** |
82 |
- * @author : 하석형 |
|
83 |
- * @since : 2023.08.24 |
|
92 |
+ * @author : 박정하 |
|
93 |
+ * @since : 2025.03.24 |
|
84 | 94 |
* @dscription : REST API 서버에 데이터 요청 보내기(Proxy) |
85 | 95 |
*/ |
86 | 96 |
webServer.use( |
87 |
- "*.json", |
|
88 |
- expressProxy(API_SERVER_HOST, { |
|
89 |
- proxyReqPathResolver: function (request) { |
|
90 |
- return `${request.params["0"]}.json`; |
|
91 |
- }, |
|
92 |
- proxyReqOptDecorator: function (proxyReqOpts, srcReq) { |
|
93 |
- proxyReqOpts.headers['X-Forwarded-For'] = srcReq.headers['x-forwarded-for'] || srcReq.connection.remoteAddress; |
|
94 |
- return proxyReqOpts; |
|
97 |
+ "*.json", |
|
98 |
+ expressProxy(API_SERVER_HOST, { |
|
99 |
+ proxyReqPathResolver: function (request) { |
|
100 |
+ // 쿼리스트링 여부에 따라 다르게 전송 |
|
101 |
+ if (request.url.split('?')[1]) { |
|
102 |
+ return request.originalUrl; |
|
103 |
+ } else { |
|
104 |
+ return `${request.params["0"]}.json`; |
|
105 |
+ } |
|
106 |
+ }, |
|
107 |
+ proxyReqOptDecorator: function (proxyReqOpts, srcReq) { |
|
108 |
+ proxyReqOpts.headers['X-Forwarded-For'] = srcReq.headers['x-forwarded-for'] || srcReq.connection.remoteAddress; |
|
109 |
+ return proxyReqOpts; |
|
95 | 110 |
} |
96 |
- }) |
|
111 |
+ }) |
|
97 | 112 |
); |
98 | 113 |
|
99 | 114 |
/** |
... | ... | @@ -102,14 +117,13 @@ |
102 | 117 |
* @dscription : REST API 서버에 데이터 요청 보내기(Proxy) |
103 | 118 |
*/ |
104 | 119 |
webServer.use( |
105 |
- "*.file", |
|
106 |
- expressProxy(API_SERVER_HOST, { |
|
107 |
- parseReqBody: false, |
|
108 |
- proxyReqPathResolver: function (request) { |
|
109 |
- console.log("request : ", request.url, request.params[0]); |
|
110 |
- return `${request.params["0"]}.file`; |
|
111 |
- }, |
|
112 |
- }) |
|
120 |
+ "*.file", |
|
121 |
+ expressProxy(API_SERVER_HOST, { |
|
122 |
+ parseReqBody: false, |
|
123 |
+ proxyReqPathResolver: function (request) { |
|
124 |
+ return `${request.params["0"]}.file`; |
|
125 |
+ }, |
|
126 |
+ }) |
|
113 | 127 |
); |
114 | 128 |
|
115 | 129 |
/** |
... | ... | @@ -118,7 +132,7 @@ |
118 | 132 |
* @dscription : ROOT URL, Router's, 화면요청 URL 등.. 이 외 나머지 정적 자원에 대한 처리 기능 |
119 | 133 |
*/ |
120 | 134 |
webServer.get("*.*", function (request, response, next) { |
121 |
- response.sendFile(`${BASE_DIR}${request.params["0"]}.${request.params["1"]}`); |
|
135 |
+ response.sendFile(`${BASE_DIR}${request.params["0"]}.${request.params["1"]}`); |
|
122 | 136 |
}); |
123 | 137 |
|
124 | 138 |
/** |
... | ... | @@ -127,11 +141,8 @@ |
127 | 141 |
* @dscription : Global Error Handler (*맨 마지막에 적용해야됨) |
128 | 142 |
*/ |
129 | 143 |
webServer.use(function (error, request, response, next) { |
130 |
- const errorCode = !error.statusCode ? 500 : error.statusCode; |
|
131 |
- response |
|
132 |
- .status(errorCode) |
|
133 |
- .send("에러가 발생하였습니다. 관리자에게 문의바랍니다."); |
|
134 |
- let message = `[Error:${errorCode}] ${request.url}/n ${error.stack}/n`; |
|
135 |
- Logger.logging(message); |
|
136 |
- //next(); |
|
137 |
-}); |
|
144 |
+ const errorCode = !error.statusCode ? 500 : error.statusCode; |
|
145 |
+ response.redirect('/'); // 메인 페이지로 유도 |
|
146 |
+ let message = `[Error: ${errorCode}]${request.url} / n ${error.stack} / n`; |
|
147 |
+ Logger.logging(message); |
|
148 |
+});(파일 끝에 줄바꿈 문자 없음) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?