@@ -35,14 +35,19 @@ Simpler possui um método de resposta que executará os métodos de resposta pad
3535
3636``` typescript
3737// Retornando com Simpler
38- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
38+ simpler .response (
39+ res ,
40+ 200 ,
41+ { " Content-Type" : " application/json" },
42+ JSON .stringify (parsedBody )
43+ );
3944
4045// Equivalente retornando diretamente com res
4146res .writeHead (200 , { " Content-Type" : " application/json" });
4247res .end (JSON .stringify (parsedBody ));
4348
4449// Retornando com Simpler
45- simpler .response (res , 200 , " text/html" , data );
50+ simpler .response (res , 200 , { " Content-Type " : " text/html" } , data );
4651
4752// Equivalente retornando diretamente com res
4853res .writeHead (200 , { " Content-Type" : " text/html" });
@@ -57,7 +62,12 @@ simpler.router.addRoute(
5762 [" POST" , " GET" ],
5863 (_req , res , body , _pathVariables , _queryParams ) => {
5964 const parsedBody = JSON .parse (body );
60- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
65+ simpler .response (
66+ res ,
67+ 200 ,
68+ { " Content-Type" : " application/json" },
69+ JSON .stringify (parsedBody )
70+ );
6171 return ;
6272 }
6373);
@@ -73,7 +83,12 @@ simpler.router.addRoute(
7383 "id": "{valor}"
7484 }
7585 */
76- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
86+ simpler .response (
87+ res ,
88+ 200 ,
89+ { " Content-Type" : " application/json" },
90+ JSON .stringify (parsedBody )
91+ );
7792 return ;
7893 }
7994);
@@ -90,7 +105,12 @@ simpler.router.addRoute(
90105 "xpto": "{valor2}",
91106 }
92107 */
93- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
108+ simpler .response (
109+ res ,
110+ 200 ,
111+ { " Content-Type" : " application/json" },
112+ JSON .stringify (parsedBody )
113+ );
94114 return ;
95115 }
96116);
@@ -110,11 +130,16 @@ simpler.router.addRoute("/static", ["GET"], (_req, res) => {
110130 const testePath = path .join (__dirname , " static" , " test.html" );
111131 readFile (testePath , (err , data ) => {
112132 if (err ) {
113- simpler .response (res , 500 , " text/plain" , " 500 Internal Server Error" );
133+ simpler .response (
134+ res ,
135+ 500 ,
136+ { " Content-Type" : " text/plain" },
137+ " 500 Internal Server Error"
138+ );
114139 return ;
115140 }
116141
117- simpler .response (res , 200 , " text/html" , data );
142+ simpler .response (res , 200 , { " Content-Type " : " text/html" } , data );
118143 });
119144});
120145```
@@ -131,6 +156,18 @@ simpler.router.addRoute("/static-page", ["GET"], (_req, res) => {
131156});
132157```
133158
159+ ### Redirecionando
160+
161+ Você pode redirectionar rotas com a função ` redirect ` , ela recebe um parametro res e a url relativa para onde deve-se redirecionar o usuário.
162+
163+ Abaixo você encontrará um exemplo de como utilizar-la.
164+
165+ ``` typescript
166+ simpler .router .addRoute (" /redir" , [" GET" ], (_req , res ) => {
167+ simpler .redirect (res , " /static-page" );
168+ });
169+ ```
170+
134171### Lidando com Erros
135172
136173Você pode lidar com erros de maneira customizada utilizando a função ` errorHandler.setCustomErrorHandler ` . A função recebe uma função como parâmetro que receberá res e error como parâmetros.
@@ -140,13 +177,14 @@ Abaixo você encontrará um exemplo de como utilizar-la.
140177``` typescript
141178simpler .errorHandler .setCustomErrorHandler (
142179 (res : ServerResponse , error : Error ) => {
143- res .writeHead (400 , { " Content-Type" : " application/json" });
180+ res .writeHead (400 , {
181+ " Content-Type" : { " Content-Type" : " application/json" },
182+ });
144183 res .end (JSON .stringify ({ message: " Custom Error" , error: error .message }));
145184 }
146185);
147186```
148187
149-
150188### Iniciando o Servidor
151189
152190Para iniciar o servidor, use o método ` listen ` . Você pode especificar o número da porta, que por padrão é 3000 se não for fornecido.
@@ -168,7 +206,12 @@ const simpler = new Simpler(true);
168206
169207simpler .router .addRoute (" /test" , [" POST" , " GET" ], (_req , res , body ) => {
170208 const parsedBody = JSON .parse (body );
171- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
209+ simpler .response (
210+ res ,
211+ 200 ,
212+ { " Content-Type" : " application/json" },
213+ JSON .stringify (parsedBody )
214+ );
172215 return ;
173216});
174217
@@ -177,7 +220,12 @@ simpler.router.addRoute(
177220 [" POST" , " GET" ],
178221 (_req , res , body : string , _pathVariables , _queryParams ) => {
179222 const parsedBody = JSON .parse (body );
180- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
223+ simpler .response (
224+ res ,
225+ 200 ,
226+ { " Content-Type" : " application/json" },
227+ JSON .stringify (parsedBody )
228+ );
181229 return ;
182230 }
183231);
@@ -187,7 +235,12 @@ simpler.router.addRoute(
187235 [" POST" , " GET" ],
188236 (_req , res , body , _pathVariables , _queryParams ) => {
189237 const parsedBody = JSON .parse (body );
190- simpler .response (res , 200 , " application/json" , JSON .stringify (parsedBody ));
238+ simpler .response (
239+ res ,
240+ 200 ,
241+ { " Content-Type" : " application/json" },
242+ JSON .stringify (parsedBody )
243+ );
191244 return ;
192245 }
193246);
@@ -216,4 +269,4 @@ Este projeto é licenciado sob a licença MIT. Consulte o arquivo [LICENSE](LICE
216269
217270# Outras versões
218271
219- [ Readme em Inglês (EN)] ( README.md )
272+ [ Readme em Inglês (EN)] ( README.md )
0 commit comments