-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
36 lines (30 loc) · 1.02 KB
/
server.js
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
const express = require('express')
const next = require('next')
const NextRedisCache = require('next-redis-cache')
const redis = require('redis')
const port = process.env.PORT || 3000
const development = process.env.NODE_ENV !== 'production'
const app = next({ dev: development })
const handler = app.getRequestHandler()
const client = redis.createClient()
/**
* Initialization of Next Redis Cache instance
*/
const nextRedisCache = new NextRedisCache(client, app, {
includes: ['/'], // routes to include for caching
})
app
.prepare()
.then(() => {
const server = express()
server.get('*',
(request, response, nxt) => nextRedisCache.middleware(request, response, nxt),
(request, response) => handler(request, response)
)
/* starting server */
return server.listen(port, error => {
if (error) throw error
console.log(`> Ready on http://localhost:${port}`)
})
})
.catch(error => new Error("Server isn't responded", error))