mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
🔥 Server Middlewares
This commit adds `middlewares` option to nuxt and accepts connect style middlewares. Also modules can register middlewares using `this.addMiddleware()` method. Each entry can be a simple middleware function or {path, handler} to support paths.
This commit is contained in:
parent
d98c24b9b9
commit
9c1060e06c
@ -59,6 +59,10 @@ class Module {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addMiddleware (middleware) {
|
||||||
|
this.options.middlewares.push(middleware)
|
||||||
|
}
|
||||||
|
|
||||||
extendBuild (fn) {
|
extendBuild (fn) {
|
||||||
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ class Nuxt {
|
|||||||
plugins: [],
|
plugins: [],
|
||||||
css: [],
|
css: [],
|
||||||
modules: [],
|
modules: [],
|
||||||
|
middlewares: [],
|
||||||
cache: false,
|
cache: false,
|
||||||
loading: {
|
loading: {
|
||||||
color: 'black',
|
color: 'black',
|
||||||
|
@ -1,15 +1,28 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const http = require('http')
|
const http = require('http')
|
||||||
|
const connect = require('connect')
|
||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
constructor (nuxt) {
|
constructor (nuxt) {
|
||||||
this.nuxt = nuxt
|
this.nuxt = nuxt
|
||||||
this.server = http.createServer(this.render.bind(this))
|
// Initialize
|
||||||
|
this.app = connect()
|
||||||
|
this.server = http.createServer(this.app)
|
||||||
|
// Add Middlewares
|
||||||
|
this.nuxt.options.middlewares.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
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
render (req, res) {
|
render (req, res, next) {
|
||||||
this.nuxt.render(req, res)
|
this.nuxt.render(req, res)
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -60,6 +60,7 @@
|
|||||||
"chokidar": "^1.6.1",
|
"chokidar": "^1.6.1",
|
||||||
"co": "^4.6.0",
|
"co": "^4.6.0",
|
||||||
"compression": "^1.6.2",
|
"compression": "^1.6.2",
|
||||||
|
"connect": "^3.6.1",
|
||||||
"css-loader": "^0.28.1",
|
"css-loader": "^0.28.1",
|
||||||
"debug": "^2.6.6",
|
"debug": "^2.6.6",
|
||||||
"extract-text-webpack-plugin": "^2.1.0",
|
"extract-text-webpack-plugin": "^2.1.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user