Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
node-sample
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
周韬
node-sample
Commits
2f479ef9
Commit
2f479ef9
authored
Apr 11, 2017
by
Evan You
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simplify usage
parent
89d33b6b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
17 deletions
+11
-17
server.js
server.js
+11
-17
No files found.
server.js
View file @
2f479ef9
...
@@ -83,7 +83,7 @@ app.get('*', (req, res) => {
...
@@ -83,7 +83,7 @@ app.get('*', (req, res) => {
res
.
setHeader
(
"Content-Type"
,
"text/html"
)
res
.
setHeader
(
"Content-Type"
,
"text/html"
)
res
.
setHeader
(
"Server"
,
serverInfo
)
res
.
setHeader
(
"Server"
,
serverInfo
)
const
errorHandle
r
=
err
=>
{
const
handleErro
r
=
err
=>
{
if
(
err
&&
err
.
code
===
404
)
{
if
(
err
&&
err
.
code
===
404
)
{
res
.
status
(
404
).
end
(
'404 | Page Not Found'
)
res
.
status
(
404
).
end
(
'404 | Page Not Found'
)
}
else
{
}
else
{
...
@@ -98,31 +98,25 @@ app.get('*', (req, res) => {
...
@@ -98,31 +98,25 @@ app.get('*', (req, res) => {
if
(
cacheable
)
{
if
(
cacheable
)
{
const
hit
=
pageCache
.
get
(
req
.
url
)
const
hit
=
pageCache
.
get
(
req
.
url
)
if
(
hit
)
{
if
(
hit
)
{
if
(
!
isProd
)
{
console
.
log
(
`cache hit!`
)
console
.
log
(
`cache hit!`
)
}
return
res
.
end
(
hit
)
return
res
.
end
(
hit
)
}
}
}
}
const
context
=
{
url
:
req
.
url
}
renderer
.
renderToString
({
url
:
req
.
url
},
(
err
,
html
)
=>
{
const
stream
=
renderer
.
renderToStream
(
context
)
if
(
err
)
{
return
handleError
(
err
)
}
res
.
end
(
html
)
if
(
cacheable
)
{
if
(
cacheable
)
{
let
content
=
''
pageCache
.
set
(
req
.
url
,
html
)
stream
.
on
(
'data'
,
chunk
=>
{
content
+=
chunk
.
toString
()
})
.
on
(
'end'
,
()
=>
{
pageCache
.
set
(
req
.
url
,
content
)
})
}
}
if
(
!
isProd
)
{
stream
.
on
(
'error'
,
errorHandler
)
.
on
(
'end'
,
()
=>
{
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
console
.
log
(
`whole request:
${
Date
.
now
()
-
s
}
ms`
)
}
})
})
.
pipe
(
res
)
})
})
const
port
=
process
.
env
.
PORT
||
8080
const
port
=
process
.
env
.
PORT
||
8080
...
...
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