2016-11-09 22:59:41 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const http = require('http')
|
2017-01-24 03:30:52 +00:00
|
|
|
const https = require('https')
|
2016-11-09 22:59:41 +00:00
|
|
|
|
|
|
|
class Server {
|
2017-01-24 03:30:52 +00:00
|
|
|
constructor (nuxt, server, isUserListen) {
|
2016-11-16 17:41:09 +00:00
|
|
|
this.nuxt = nuxt
|
2017-01-24 03:30:52 +00:00
|
|
|
if (server instanceof http.Server && server instanceof https.Server) {
|
|
|
|
this.isUserListen = isUserListen === void 0 ? true : isUserListen
|
|
|
|
this.server = server
|
|
|
|
} else {
|
|
|
|
this.isUserListen = false
|
|
|
|
this.server = http.createServer()
|
|
|
|
}
|
|
|
|
this.server.on('request', this.render.bind(this))
|
2016-11-16 17:41:09 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
render (req, res) {
|
|
|
|
this.nuxt.render(req, res)
|
2016-11-09 22:59:41 +00:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
listen (port, host) {
|
2017-01-24 03:30:52 +00:00
|
|
|
if (this.isUserListen !== true) {
|
|
|
|
host = host || 'localhost'
|
|
|
|
port = port || 3000
|
|
|
|
this.server.listen(port, host, () => {
|
|
|
|
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
console.log('Please use the incoming server to establish listen') // eslint-disable-line no-console
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:41:09 +00:00
|
|
|
return this
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 22:07:18 +00:00
|
|
|
close (cb) {
|
|
|
|
return this.server.close(cb)
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
export default Server
|