-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.js
50 lines (42 loc) · 1.28 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const path = require('path')
const express = require('express')
const compression = require('compression')
const next = require('next')
const firebase = require('firebase')
const hackernews = require('firebase-hackernews')
const dev = process.env.NODE_ENV !== 'production'
const app = next({dev})
const handle = app.getRequestHandler()
const serve = (subpath, cache) => express.static(
path.resolve(__dirname, subpath),
{maxAge: cache && !dev ? 1000 * 60 * 60 * 24 * 30 : 0}
)
const hnservice = hackernews.init(firebase)
app.prepare()
.then(() => hnservice.watch())
.then(() => {
const server = express()
server.use(compression({threshold: 0}))
server.use('/static', serve('./static', true))
server.use('/service-worker.js', serve('./.next/service-worker.js', true))
server.use('/manifest.json', serve('./static/manifest.json', true))
server.get('/hackernews/*', (req, res) => {
hnservice.fetch(req.path)
.then(data => {
res.send(typeof data === 'number' ? String(data) : data)
})
.catch(err => {
console.error(err)
res.status(500).send(err.toString())
})
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(3000, err => {
if (err) {
throw err
}
console.log('> Ready on http://localhost:3000')
})
})