1- import React from "react" ;
1+ import React , { Children } from "react" ;
22import { renderToString } from "react-dom/server" ;
33import { ServerStyleSheet } from "styled-components" ;
44import { Helmet } from "react-helmet" ;
55import { ReqResContext } from "../context" ;
6+ import { Res } from "../components" ;
67
78const sheet = new ServerStyleSheet ( ) ;
89
@@ -21,6 +22,8 @@ function renderPage(Component, req, res) {
2122 <!DOCTYPE html>
2223 <html ${ helmet . htmlAttributes . toString ( ) } >
2324 <head>
25+ <meta charset="UTF-8">
26+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
2427 ${ helmet . title . toString ( ) }
2528 ${ helmet . meta . toString ( ) }
2629 ${ helmet . link . toString ( ) }
@@ -37,20 +40,53 @@ function renderPage(Component, req, res) {
3740 res . send ( html ) ;
3841}
3942
43+ function paramfn ( sq , req , res , next ) {
44+ for ( const param of sq ) {
45+ switch ( param . type ) {
46+ case "json" :
47+ res . send ( param . content ) ;
48+ break ;
49+ case "text" :
50+ res . send ( param . content ) ;
51+ break ;
52+ case "status" :
53+ res . statusCode = param . content ;
54+ break ;
55+ case "contentType" :
56+ res . setHeader ( "Content-Type" , param . content ) ;
57+ break ;
58+ case "redirect" :
59+ if ( param . content . statusCode )
60+ res . redirect ( param . content . statusCode , param . content . path ) ;
61+ else res . redirect ( param . content . path ) ;
62+ res . end ( ) ;
63+ break ;
64+ case "render" :
65+ renderPage ( param . content , req , res ) ;
66+ break ;
67+ case "send-file" :
68+ res . sendFile ( param . content . path , param . content . options , ( err ) => {
69+ if ( err ) {
70+ param . content . onError ( err ) ;
71+ next ( ) ;
72+ }
73+ } ) ;
74+ break ;
75+ default :
76+ }
77+ }
78+ }
79+
4080export function generateRoute ( parentInstance , props ) {
4181 parentInstance . routerInstance [ props . method ] (
4282 props . path || "/" ,
4383 ...[
4484 ...( props . middlewares || [ ] ) ,
4585 async ( req , res , next ) => {
46- if ( props . content ) {
47- if ( props . status ) res . status ( props . status ) ;
48- if ( typeof props . content === "function" ) {
49- renderPage ( props . content , req , res ) ;
50- } else {
51- res . send ( props . content ) ;
52- }
86+ if ( props . paramsSeq ) {
87+ paramfn ( props . paramsSeq , req , res , next ) ;
5388 }
89+
5490 if ( props . handler )
5591 await props . handler ( req , res , next , ( Component ) =>
5692 renderPage ( Component , req , res )
0 commit comments