import hooks from './hooks'
export default {
router: {
base: '/portal'
hooks: hooks(this)
然后,创建一些文件;
hooks/index.js
, Hooks 模块
import render from './render'
export default nuxtConfig => ({
render: render(nuxtConfig)
hooks/render.js
, 渲染 hook
import redirectRootToPortal from './route-redirect-portal'
export default nuxtConfig => {
const router = Reflect.has(nuxtConfig, 'router') ? nuxtConfig.router : {}
const base = Reflect.has(router, 'base') ? router.base : '/portal'
return {
* 'render:setupMiddleware'
* {@link node_modules/nuxt/lib/core/renderer.js}
setupMiddleware(app) {
app.use('/', redirectRootToPortal(base))
hooks/route-redirect-portal.js
, 中间件本身
* Nuxt middleware hook to redirect from / to /portal (or whatever we set in nuxt.config.js router.base)
* Should be the same version as connect
* {@link node_modules/connect/package.json}
import parseurl from 'parseurl'
* Connect middleware to handle redirecting to desired Web Applicatin Context Root.
* Notice that Nuxt docs lacks explaning how to use hooks.
* This is a sample router to help explain.
* See nice implementation for inspiration:
* - https://github.com/nuxt/nuxt.js/blob/dev/examples/with-cookies/plugins/cookies.js
* - https://github.com/yyx990803/launch-editor/blob/master/packages/launch-editor-middleware/index.js
* [http_class_http_clientrequest]: https://nodejs.org/api/http.html#http_class_http_clientrequest
* [http_class_http_serverresponse]: https://nodejs.org/api/http.html#http_class_http_serverresponse
* @param {http.ClientRequest} req Node.js internal client request object [http_class_http_clientrequest]
* @param {http.ServerResponse} res Node.js internal response [http_class_http_serverresponse]
* @param {Function} next middleware callback
export default desiredContextRoot =>
function projectHooksRouteRedirectPortal(req, res, next) {
const desiredContextRootRegExp = new RegExp(`^${desiredContextRoot}`)
const _parsedUrl = Reflect.has(req, '_parsedUrl') ? req._parsedUrl : null
const url = _parsedUrl !== null ? _parsedUrl : parseurl(req)
const startsWithDesired = desiredContextRootRegExp.test(url.pathname)
const isNotProperContextRoot = desiredContextRoot !== url.pathname
if (isNotProperContextRoot && startsWithDesired === false) {
const pathname = url.pathname === null ? '' : url.pathname
const search = url.search === null ? '' : url.search
const Location = desiredContextRoot + pathname + search
res.writeHead(302, {
Location
res.end()
next()
然后,每当开发中的同事到达开发 Web 开发服务/
时,发生了意外情况,Nuxt 将自动重定向到/portal