Nuxt/lib/server.js

45 lines
959 B
JavaScript
Raw Normal View History

'use strict'
const http = require('http')
const connect = require('connect')
class Server {
constructor (nuxt) {
this.nuxt = nuxt
// Initialize
this.app = connect()
this.server = http.createServer(this.app)
// Add Middlewares
this.nuxt.options.serverMiddlewares.forEach(m => {
if (m instanceof Function) {
this.app.use(m)
} else if (m && m.path && m.handler) {
this.app.use(m.path, m.handler)
}
})
// Add default render middleware
this.app.use(this.render.bind(this))
return this
}
render (req, res, next) {
this.nuxt.render(req, res)
return this
}
listen (port, host) {
host = host || '127.0.0.1'
port = port || 3000
this.server.listen(port, host, () => {
2016-12-08 15:41:20 +00:00
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console
})
return this
}
2016-12-09 22:07:18 +00:00
close (cb) {
return this.server.close(cb)
}
}
2017-01-11 19:14:59 +00:00
export default Server