rsnext/examples/custom-server-actionhero/initializers/next.js

37 lines
897 B
JavaScript
Raw Normal View History

2018-02-27 13:16:17 +01:00
'use strict'
const { Initializer, api } = require('actionhero')
2018-02-27 13:16:17 +01:00
const next = require('next')
module.exports = class NextInitializer extends Initializer {
constructor () {
super()
this.name = 'next'
}
async initialize () {
api.next = {
render: async connection => {
if (connection.type !== 'web') {
throw new Error('Connections for NEXT apps must be of type "web"')
}
2018-02-27 13:16:17 +01:00
const req = connection.rawConnection.req
const res = connection.rawConnection.res
return api.next.handle(req, res)
}
}
api.next.dev = api.env === 'development'
if (api.next.dev) {
api.log('Running next in development mode...')
}
2018-02-27 13:16:17 +01:00
api.next.app = next({ dev: api.next.dev })
2018-02-27 13:16:17 +01:00
api.next.handle = api.next.app.getRequestHandler()
await api.next.app.prepare()
}
async stop () {
await api.next.app.close()
}
}