@@ -7,49 +7,59 @@ description: カスタムサーバーを使用してプログラム上で Next.j
77<details >
88 <summary ><b >Examples</b ></summary >
99 <ul >
10- <li><a href="https://github.com/vercel/next.js/tree/canary/examples/custom-server">Basic custom server</a></li>
1110 <li><a href="https://github.com/vercel/next.js/tree/canary/examples/custom-server-express">Express integration</a></li>
1211 <li><a href="https://github.com/vercel/next.js/tree/canary/examples/custom-server-hapi">Hapi integration</a></li>
1312 <li><a href="https://github.com/vercel/next.js/tree/canary/examples/custom-server-koa">Koa integration</a></li>
1413 <li><a href="https://github.com/vercel/next.js/tree/canary/examples/ssr-caching">SSR Caching</a></li>
1514 </ul >
1615</details >
1716
18- 通常、next サーバーは ` next start ` で起動します。しかしながら、カスタムしたルーティングパターンを使用するために、100% プログラム上での起動が可能です。
17+ Next.js は ` next start ` で起動される独自のサーバーをデフォルトで含んでいます。もし既存のバックエンドがある場合でも、それを Next.js において利用し続けられます(これはカスタムサーバーではありません)。カスタム Next.js サーバーはカスタムされたサーバーパターンを利用するために 100% プログラムで制御してサーバーを起動することを可能にします。ほとんどの場合必要ではありませんが、完全なカスタマイズのために利用可能です。
18+
19+ > 注意: カスタムサーバーは Vercel へとデプロイすることができません。
1920
2021> カスタムサーバーの使用を決定する前に、Next.js の統合ルーターがアプリの要件を満たせない場合にのみ使用すべきことを気に留めてください。カスタムサーバーでは、** サーバーレス関数** や ** [ Automatic Static Optimization] ( /docs/advanced-features/automatic-static-optimization.md ) .** などの重要なパフォーマンス最適化が削除されます。
2122
2223次のカスタムサーバーの例を見てください:
2324
2425``` js
2526// server.js
26- const { createServer } = require (' http' );
27- const { parse } = require (' url' );
28- const next = require (' next' );
27+ const { createServer } = require (' http' )
28+ const { parse } = require (' url' )
29+ const next = require (' next' )
2930
30- const dev = process .env .NODE_ENV !== ' production' ;
31- const app = next ({ dev });
32- const handle = app .getRequestHandler ();
31+ const dev = process .env .NODE_ENV !== ' production'
32+ const hostname = ' localhost'
33+ const port = 3000
34+ // ミドルウェアを利用する場合、 `hostname` と `port` を以下のように提供する必要があります。
35+ const app = next ({ dev, hostname, port })
36+ const handle = app .getRequestHandler ()
3337
3438app .prepare ().then (() => {
35- createServer ((req , res ) => {
36- // `url.parse` の2番目の引数として必ず `true` を渡してください。
37- // これは、URLのクエリ部分を解析するように指示します。
38- const parsedUrl = parse (req .url , true );
39- const { pathname , query } = parsedUrl;
40-
41- if (pathname === ' /a' ) {
42- app .render (req, res, ' /a' , query);
43- } else if (pathname === ' /b' ) {
44- app .render (req, res, ' /b' , query);
45- } else {
46- handle (req, res, parsedUrl);
39+ createServer (async (req , res ) => {
40+ try {
41+ // `url.parse` の2番目の引数として必ず `true` を渡してください。
42+ // これは、URLのクエリ部分を解析するように指示します。
43+ const parsedUrl = parse (req .url , true )
44+ const { pathname , query } = parsedUrl
45+
46+ if (pathname === ' /a' ) {
47+ await app .render (req, res, ' /a' , query)
48+ } else if (pathname === ' /b' ) {
49+ await app .render (req, res, ' /b' , query)
50+ } else {
51+ await handle (req, res, parsedUrl)
52+ }
53+ } catch (err) {
54+ console .error (' Error occurred handling' , req .url , err)
55+ res .statusCode = 500
56+ res .end (' internal server error' )
4757 }
48- }).listen (3000 , err => {
49- if (err) throw err;
50- console .log (' > Ready on http://localhost:3000 ' );
51- });
52- });
58+ }).listen (port, ( err ) => {
59+ if (err) throw err
60+ console .log (` > Ready on http://${ hostname } : ${ port } ` )
61+ })
62+ })
5363```
5464
5565> ` server.js ` は babel や webpack を経由しません。このファイルに必要な構文とソースが、現在実行中の node バージョンと互換性があることを確認してください。
0 commit comments