1
1
import { resolve , join } from 'path'
2
2
import { parse } from 'url'
3
3
import fs from 'mz/fs'
4
- import http from 'http'
4
+ import http , { STATUS_CODES } from 'http'
5
5
import {
6
6
renderToHTML ,
7
7
renderErrorToHTML ,
@@ -38,7 +38,7 @@ export default class Server {
38
38
. catch ( ( err ) => {
39
39
if ( ! this . quiet ) console . error ( err )
40
40
res . statusCode = 500
41
- res . end ( 'error' )
41
+ res . end ( STATUS_CODES [ 500 ] )
42
42
} )
43
43
}
44
44
}
@@ -67,43 +67,52 @@ export default class Server {
67
67
}
68
68
69
69
defineRoutes ( ) {
70
- this . router . get ( '/_next-prefetcher.js' , async ( req , res , params ) => {
71
- const p = join ( __dirname , '../client/next-prefetcher-bundle.js' )
72
- await serveStatic ( req , res , p )
73
- } )
74
-
75
- this . router . get ( '/_next/:buildId/main.js' , async ( req , res , params ) => {
76
- this . handleBuildId ( params . buildId , res )
77
- const p = join ( this . dir , '.next/main.js' )
78
- await serveStaticWithGzip ( req , res , p )
79
- } )
80
-
81
- this . router . get ( '/_next/:buildId/commons.js' , async ( req , res , params ) => {
82
- this . handleBuildId ( params . buildId , res )
83
- const p = join ( this . dir , '.next/commons.js' )
84
- await serveStaticWithGzip ( req , res , p )
85
- } )
86
-
87
- this . router . get ( '/_next/:buildId/pages/:path*' , async ( req , res , params ) => {
88
- this . handleBuildId ( params . buildId , res )
89
- const paths = params . path || [ 'index' ]
90
- const pathname = `/${ paths . join ( '/' ) } `
91
- await this . renderJSON ( req , res , pathname )
92
- } )
93
-
94
- this . router . get ( '/_next/:path+' , async ( req , res , params ) => {
95
- const p = join ( __dirname , '..' , 'client' , ...( params . path || [ ] ) )
96
- await serveStatic ( req , res , p )
97
- } )
98
- this . router . get ( '/static/:path+' , async ( req , res , params ) => {
99
- const p = join ( this . dir , 'static' , ...( params . path || [ ] ) )
100
- await serveStatic ( req , res , p )
101
- } )
70
+ const routes = {
71
+ '/_next-prefetcher.js' : async ( req , res , params ) => {
72
+ const p = join ( __dirname , '../client/next-prefetcher-bundle.js' )
73
+ await this . serveStatic ( req , res , p )
74
+ } ,
75
+
76
+ '/_next/:buildId/main.js' : async ( req , res , params ) => {
77
+ this . handleBuildId ( params . buildId , res )
78
+ const p = join ( this . dir , '.next/main.js' )
79
+ await this . serveStaticWithGzip ( req , res , p )
80
+ } ,
81
+
82
+ '/_next/:buildId/commons.js' : async ( req , res , params ) => {
83
+ this . handleBuildId ( params . buildId , res )
84
+ const p = join ( this . dir , '.next/commons.js' )
85
+ await this . serveStaticWithGzip ( req , res , p )
86
+ } ,
87
+
88
+ '/_next/:buildId/pages/:path*' : async ( req , res , params ) => {
89
+ this . handleBuildId ( params . buildId , res )
90
+ const paths = params . path || [ 'index' ]
91
+ const pathname = `/${ paths . join ( '/' ) } `
92
+ await this . renderJSON ( req , res , pathname )
93
+ } ,
94
+
95
+ '/_next/:path+' : async ( req , res , params ) => {
96
+ const p = join ( __dirname , '..' , 'client' , ...( params . path || [ ] ) )
97
+ await this . serveStatic ( req , res , p )
98
+ } ,
99
+
100
+ '/static/:path+' : async ( req , res , params ) => {
101
+ const p = join ( this . dir , 'static' , ...( params . path || [ ] ) )
102
+ await this . serveStatic ( req , res , p )
103
+ } ,
104
+
105
+ '/:path*' : async ( req , res ) => {
106
+ const { pathname, query } = parse ( req . url , true )
107
+ await this . render ( req , res , pathname , query )
108
+ }
109
+ }
102
110
103
- this . router . get ( '/:path*' , async ( req , res ) => {
104
- const { pathname, query } = parse ( req . url , true )
105
- await this . render ( req , res , pathname , query )
106
- } )
111
+ for ( const method of [ 'GET' , 'HEAD' ] ) {
112
+ for ( const p of Object . keys ( routes ) ) {
113
+ this . router . add ( method , p , routes [ p ] )
114
+ }
115
+ }
107
116
}
108
117
109
118
async start ( port ) {
@@ -125,8 +134,14 @@ export default class Server {
125
134
const fn = this . router . match ( req , res )
126
135
if ( fn ) {
127
136
await fn ( )
128
- } else {
137
+ return
138
+ }
139
+
140
+ if ( req . method === 'GET' || req . method === 'HEAD' ) {
129
141
await this . render404 ( req , res )
142
+ } else {
143
+ res . statusCode = 501
144
+ res . end ( STATUS_CODES [ 501 ] )
130
145
}
131
146
}
132
147
@@ -135,7 +150,7 @@ export default class Server {
135
150
res . setHeader ( 'X-Powered-By' , `Next.js ${ pkg . version } ` )
136
151
}
137
152
const html = await this . renderToHTML ( req , res , pathname , query )
138
- sendHTML ( res , html )
153
+ sendHTML ( res , html , req . method )
139
154
}
140
155
141
156
async renderToHTML ( req , res , pathname , query ) {
@@ -163,7 +178,7 @@ export default class Server {
163
178
164
179
async renderError ( err , req , res , pathname , query ) {
165
180
const html = await this . renderErrorToHTML ( err , req , res , pathname , query )
166
- sendHTML ( res , html )
181
+ sendHTML ( res , html , req . method )
167
182
}
168
183
169
184
async renderErrorToHTML ( err , req , res , pathname , query ) {
@@ -191,7 +206,7 @@ export default class Server {
191
206
async render404 ( req , res ) {
192
207
const { pathname, query } = parse ( req . url , true )
193
208
res . statusCode = 404
194
- this . renderErrorToHTML ( null , req , res , pathname , query )
209
+ this . renderError ( null , req , res , pathname , query )
195
210
}
196
211
197
212
async renderJSON ( req , res , page ) {
0 commit comments