-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
200 lines (157 loc) · 5.59 KB
/
server.js
File metadata and controls
200 lines (157 loc) · 5.59 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// const next = require('next')
// const cacheableResponse = require('cacheable-response')
// const helmet = require('helmet')
// const featurePolicy = require('feature-policy')
// const dev = process.env.NODE_ENV !== 'production'
// const app = next({ dev })
// const express = require('express')
// const server = express()
// const handle = app.getRequestHandler()
// const ssrCache = cacheableResponse({
// ttl: 1000 * 60 * 60, // 1hour
// get: async ({ req, res, pagePath, queryParams }) => ({
// data: await app.renderToHTML(req, res, pagePath, queryParams)
// }),
// send: ({ data, res }) => res.send(data)
// })
// server.set('trust proxy', true);
// Header security. See: https://observatory.mozilla.org/
// server.use(helmet());
// //Sets "Referrer-Policy: same-origin".
// server.use(helmet.referrerPolicy({ policy: 'same-origin' }));
// // Sets Feature-policy
// server.use(featurePolicy({
// features: {
// fullscreen: ["'self'"],
// vibrate: ["'none'"],
// payment: ['https://yyy.com'],
// syncXhr: ["'self'"],
// geolocation: ["'self'"]
// }
// }));
// app.prepare().then(() => {
// server.get('*', function(req,res,next) {
// if(req.headers['x-forwarded-proto'] != 'https' && process.env.NODE_ENV === 'production')
// res.redirect('https://'+req.hostname+req.url)
// else
// next() /* Continue to other routes if we're not redirecting */
// });
// server.get('/', (req, res) => ssrCache({ req, res, pagePath: '/' }))
// server.get('*', (req, res) => handle(req, res))
// server.listen((port, err) => {
// if (err) throw err
// console.log(`> Ready on http://localhost:3000`)
// })
// })
const express = require('express')
const next = require('next')
const { parse } = require('url')
const requestIp = require('request-ip') // import it at the top
const { createServer } = require('http')
const { hostname } = require('os')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const server = express()
console.log({ here: 'here' })
app.prepare().then(() => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
ip = requestIp.getClientIp(req)
console.log({ headers:req.headers })
if (pathname === '/') {
console.log({ clientIpserver: ip })
app.render({ ...req, clientIp: ip, started:'yes' }, res, '/', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
handle(req, res, parsedUrl)
}
}).listen(port,hostname, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:'+port, {hostname})
})
// const server = express()
// server.all('*', (req, res) => {
// return handle(req, res)
// })
// server.listen(port, err => {
// if (err) throw err
// console.log(`> Ready on http://localhost:${port}`)
// })
// server.get('/', (req, res) => {
// ip = requestIp.getClientIp(req)
// console.log({clientIpserver:ip})
// app.render(
// {
// ...req,
// clientIP: ip
// },
// res,
// '/index',
// req.query
// )
// })
// server.all('*', (req, res) => {
// return handle(req, res)
// })
})
// const express = require('express')
// const next = require('next')
// const { parse } = require('url')
// const requestIp = require('request-ip') // import it at the top
// const { createServer } = require('http')
// const port = parseInt(process.env.PORT, 10) || 3000
// const dev = process.env.NODE_ENV !== 'production'
// const app = next({ dev })
// const handle = app.getRequestHandler()
// const server = express()
// console.log({ here: 'here' })
// app.prepare().then(() => {
// createServer((req, res) => {
// // Be sure to pass `true` as the second argument to `url.parse`.
// // This tells it to parse the query portion of the URL.
// const parsedUrl = parse(req.url, true)
// const { pathname, query } = parsedUrl
// ip = requestIp.getClientIp(req)
// if (pathname === '/') {
// console.log({ clientIpserver: ip })
// app.render({ ...req, clientIp: ip }, res, '/', query)
// } else if (pathname === '/b') {
// app.render(req, res, '/b', query)
// } else {
// handle(req, res, parsedUrl)
// }
// }).listen(3000, (err) => {
// if (err) throw err
// console.log('> Ready on http://localhost:3000')
// })
// // const server = express()
// // server.all('*', (req, res) => {
// // return handle(req, res)
// // })
// // server.listen(port, err => {
// // if (err) throw err
// // console.log(`> Ready on http://localhost:${port}`)
// // })
// // server.get('/', (req, res) => {
// // ip = requestIp.getClientIp(req)
// // console.log({clientIpserver:ip})
// // app.render(
// // {
// // ...req,
// // clientIP: ip
// // },
// // res,
// // '/index',
// // req.query
// // )
// // })
// // server.all('*', (req, res) => {
// // return handle(req, res)
// // })
// })