Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
e8f39ad7
Commit
e8f39ad7
authored
Dec 05, 2017
by
Tibor Vukovic
Committed by
Tim Neutkens
Dec 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update hapi example to support hapi@17 (#3357)
parent
c1cefad2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
57 deletions
+52
-57
next-wrapper.js
examples/custom-server-hapi/next-wrapper.js
+16
-10
package.json
examples/custom-server-hapi/package.json
+2
-4
server.js
examples/custom-server-hapi/server.js
+34
-43
No files found.
examples/custom-server-hapi/next-wrapper.js
View file @
e8f39ad7
const
pathWrapper
=
(
app
,
pathName
,
opts
)
=>
({
raw
,
query
},
hapiReply
)
=>
app
.
renderToHTML
(
raw
.
req
,
raw
.
res
,
pathName
,
query
,
opts
)
.
then
(
hapiReply
)
const
{
parse
}
=
require
(
'url'
)
const
defaul
tHandlerWrapper
=
app
=>
{
const
nex
tHandlerWrapper
=
app
=>
{
const
handler
=
app
.
getRequestHandler
()
return
({
raw
,
url
},
hapiReply
)
=>
handler
(
raw
.
req
,
raw
.
res
,
url
)
.
then
(()
=>
{
hapiReply
.
close
(
false
)
})
return
async
({
raw
,
url
},
h
)
=>
{
await
handler
(
raw
.
req
,
raw
.
res
,
url
)
return
h
.
close
}
}
module
.
exports
=
{
pathWrapper
,
defaultHandlerWrapper
}
const
defaultHandlerWrapper
=
app
=>
async
({
raw
:
{
req
,
res
},
url
})
=>
{
const
{
pathname
,
query
}
=
parse
(
url
,
true
)
return
app
.
renderToHTML
(
req
,
res
,
pathname
,
query
)
}
const
pathWrapper
=
(
app
,
pathName
,
opts
)
=>
async
({
raw
,
query
,
params
})
=>
{
return
app
.
renderToHTML
(
raw
.
req
,
raw
.
res
,
pathName
,
{
...
query
,
...
params
},
opts
)
}
module
.
exports
=
{
pathWrapper
,
defaultHandlerWrapper
,
nextHandlerWrapper
}
examples/custom-server-hapi/package.json
View file @
e8f39ad7
...
...
@@ -7,11 +7,9 @@
"start"
:
"NODE_ENV=production node server.js"
},
"dependencies"
:
{
"hapi"
:
"^1
6.1.0
"
,
"hapi"
:
"^1
7.1.1
"
,
"next"
:
"latest"
,
"react"
:
"^16.0.0"
,
"react-dom"
:
"^16.0.0"
,
"good"
:
"^7.1.0"
,
"good-console"
:
"^6.2.0"
"react-dom"
:
"^16.0.0"
}
}
examples/custom-server-hapi/server.js
View file @
e8f39ad7
const
next
=
require
(
'next'
)
const
Hapi
=
require
(
'hapi'
)
const
Good
=
require
(
'good'
)
const
{
pathWrapper
,
defaultHandlerWrapper
}
=
require
(
'./next-wrapper'
)
const
{
pathWrapper
,
defaultHandlerWrapper
,
nextHandlerWrapper
}
=
require
(
'./next-wrapper'
)
const
port
=
parseInt
(
process
.
env
.
PORT
,
10
)
||
3000
const
dev
=
process
.
env
.
NODE_ENV
!==
'production'
const
app
=
next
({
dev
})
const
server
=
new
Hapi
.
Server
()
// add request logging (optional)
const
pluginOptions
=
[
{
register
:
Good
,
options
:
{
reporters
:
{
console
:
[{
module
:
'good-console'
},
'stdout'
]
}
}
}
]
const
server
=
new
Hapi
.
Server
({
port
})
app
.
prepare
()
.
then
(()
=>
{
server
.
connection
({
port
})
server
.
register
(
pluginOptions
)
.
then
(()
=>
{
server
.
route
({
method
:
'GET'
,
path
:
'/a'
,
handler
:
pathWrapper
(
app
,
'/a'
)
})
app
.
prepare
()
.
then
(
async
()
=>
{
server
.
route
({
method
:
'GET'
,
path
:
'/a'
,
handler
:
pathWrapper
(
app
,
'/a'
)
})
server
.
route
({
method
:
'GET'
,
path
:
'/b'
,
handler
:
pathWrapper
(
app
,
'/b'
)
})
server
.
route
({
method
:
'GET'
,
path
:
'/b'
,
handler
:
pathWrapper
(
app
,
'/b'
)
})
server
.
route
({
method
:
'GET'
,
path
:
'/{p*}'
,
/* catch all route
*/
handler
:
defaul
tHandlerWrapper
(
app
)
})
server
.
route
({
method
:
'GET'
,
path
:
'/_next/{p*}'
,
/* next specific routes
*/
handler
:
nex
tHandlerWrapper
(
app
)
})
server
.
start
().
catch
(
error
=>
{
console
.
log
(
'Error starting server'
)
console
.
log
(
error
)
}).
then
(()
=>
{
console
.
log
(
`> Ready on http://localhost:
${
port
}
`
)
})
server
.
route
({
method
:
'GET'
,
path
:
'/{p*}'
,
/* catch all route */
handler
:
defaultHandlerWrapper
(
app
)
})
try
{
await
server
.
start
()
console
.
log
(
`> Ready on http://localhost:
${
port
}
`
)
}
catch
(
error
)
{
console
.
log
(
'Error starting server'
)
console
.
log
(
error
)
}
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment