FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hackernews-react-graphql/src/server.ts at master · codekea/hackernews-react-graphql · GitHub
codekea
/
hackernews-react-graphql
Public
forked from
clintonwoo/hackernews-react-graphql
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hackernews-react-graphql
/
src
/
server.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
165 lines (145 loc) · 4.68 KB
Breadcrumbs
hackernews-react-graphql
/
src
/
server.ts
Copy path
File metadata and controls
165 lines (145 loc) · 4.68 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import
{
ApolloServer
}
from
'apollo-server-express'
;
import
*
as
bodyParser
from
'body-parser'
;
import
cookieParser
from
'cookie-parser'
;
import
express
from
'express'
;
import
session
from
'express-session'
;
import
nextApp
from
'next'
;
import
passport
from
'passport'
;
import
{
Strategy
as
LocalStrategy
}
from
'passport-local'
;
import
{
seedCache
}
from
'./data/hn-data-api'
;
import
{
Comment
,
FeedSingleton
,
NewsItem
,
User
}
from
'./data/models'
;
import
{
resolvers
,
typeDefs
}
from
'./data/schema'
;
import
{
APP_PORT
,
APP_URI
,
dev
,
GRAPHQL_URL
,
graphQLPath
,
useGraphqlPlayground
}
from
'./config'
;
// Seed the in-memory data using the HN api
const
delay
=
dev
?
/* 1000 * 60 * 1 1 minute */
0
:
0
;
seedCache
(
delay
)
;
const
app
=
nextApp
(
{
dev
}
)
;
const
handle
=
app
.
getRequestHandler
(
)
;
app
.
prepare
(
)
.
then
(
(
)
=>
{
const
expressServer
=
express
(
)
;
/* BEGIN PASSPORT.JS AUTHENTICATION */
passport
.
use
(
new
LocalStrategy
(
{
usernameField
:
'id'
,
}
,
async
(
username
,
password
,
done
)
=>
{
const
user
=
await
User
.
getUser
(
username
)
;
if
(
!
user
)
{
return
done
(
null
,
false
,
{
message
:
'Incorrect username.'
}
)
;
}
if
(
!
(
await
User
.
validPassword
(
username
,
password
)
)
)
{
return
done
(
null
,
false
,
{
message
:
'Incorrect password.'
}
)
;
}
return
done
(
null
,
user
)
;
}
)
)
;
/*
In this example, only the user ID is serialized to the session,
keeping the amount of data stored within the session small. When
subsequent requests are received, this ID is used to find the user,
which will be restored to req.user.
*/
passport
.
serializeUser
(
(
user
:
any
/* TODO: TYPE */
,
cb
)
=>
{
cb
(
null
,
user
.
id
)
;
}
)
;
passport
.
deserializeUser
(
async
(
id
,
cb
)
=>
{
const
user
=
await
User
.
getUser
(
id
)
;
cb
(
null
,
user
||
null
)
;
}
)
;
expressServer
.
use
(
cookieParser
(
'mysecret'
)
)
;
expressServer
.
use
(
session
(
{
cookie
:
{
maxAge
:
1000
*
60
*
60
*
24
*
7
}
,
// Requires https: secure: false
resave
:
false
,
rolling
:
true
,
saveUninitialized
:
false
,
secret
:
'mysecret'
,
}
)
)
;
expressServer
.
use
(
passport
.
initialize
(
)
)
;
expressServer
.
use
(
bodyParser
.
urlencoded
(
{
extended
:
false
}
)
)
;
expressServer
.
use
(
passport
.
session
(
)
)
;
expressServer
.
post
(
'/login'
,
(
req
,
res
,
next
)
=>
{
req
.
session
.
returnTo
=
req
.
body
.
goto
;
next
(
)
;
}
,
passport
.
authenticate
(
'local'
,
{
failureRedirect
:
'/login?how=unsuccessful'
,
successReturnToOrRedirect
:
'/'
,
}
)
)
;
expressServer
.
post
(
'/register'
,
async
(
req
,
res
,
next
)
=>
{
if
(
!
req
.
user
)
{
try
{
await
User
.
registerUser
(
{
id
:
req
.
body
.
id
,
password
:
req
.
body
.
password
,
}
)
;
req
.
session
.
returnTo
=
`/user?id=
${
req
.
body
.
id
}
`
;
}
catch
(
err
)
{
req
.
session
.
returnTo
=
`/login?how=
${
err
.
code
}
`
;
}
}
else
{
req
.
session
.
returnTo
=
'/login?how=user'
;
}
next
(
)
;
}
,
passport
.
authenticate
(
'local'
,
{
failureRedirect
:
'/login?how=unsuccessful'
,
successReturnToOrRedirect
:
'/'
,
}
)
)
;
expressServer
.
get
(
'/logout'
,
(
req
,
res
)
=>
{
req
.
logout
(
)
;
res
.
redirect
(
'/'
)
;
}
)
;
/* END PASSPORT.JS AUTHENTICATION */
/* BEGIN GRAPHQL */
const
apolloServer
=
new
ApolloServer
(
{
context
:
(
{
req
}
)
=>
(
{
Comment
,
Feed
:
FeedSingleton
,
NewsItem
,
User
,
userId
:
req
.
user
&&
req
.
user
.
id
,
}
)
,
introspection
:
true
,
playground
:
useGraphqlPlayground
,
resolvers
,
typeDefs
,
}
)
;
apolloServer
.
applyMiddleware
(
{
app
:
expressServer
,
path
:
graphQLPath
}
)
;
/* END GRAPHQL */
/* BEGIN EXPRESS ROUTES */
// This is how to render a masked route with NextJS
// server.get('/p/:id', (req, res) => {
// const actualPage = '/post';
// const queryParams = { id: req.params.id };
// app.render(req, res, actualPage, queryParams);
// });
expressServer
.
get
(
'/news'
,
(
req
,
res
)
=>
{
const
actualPage
=
'/'
;
app
.
render
(
req
,
res
,
actualPage
)
;
}
)
;
expressServer
.
get
(
'*'
,
(
req
,
res
)
=>
handle
(
req
,
res
)
)
;
/* END EXPRESS ROUTES */
expressServer
.
listen
(
APP_PORT
,
(
)
=>
{
console
.
log
(
`> App ready on
${
APP_URI
}
`
)
;
console
.
log
(
`> GraphQL ready on
${
GRAPHQL_URL
}
`
)
;
console
.
log
(
`> GraphQL Playground is
${
useGraphqlPlayground
?
' '
:
' not '
}
enabled`
)
;
console
.
log
(
`Dev:
${
dev
}
`
)
;
}
)
;
}
)
.
catch
(
err
=>
{
console
.
error
(
err
.
stack
)
;
process
.
exit
(
1
)
;
}
)
;
export
default
app
;
Back
|
FazBrowse Home
|
New Git URL