Skip to content

Commit

Permalink
feat: use think nuxt middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
baisheng committed Jul 24, 2018
1 parent 749341b commit a84e747
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .build/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Router from 'vue-router'

Vue.use(Router)

const _0232240e = () => import('../client/pages/index.vue' /* webpackChunkName: "pages/index" */).then(m => m.default || m)
const _62267d94 = () => import('../client/pages/index.vue' /* webpackChunkName: "pages/index" */).then(m => m.default || m)



Expand Down Expand Up @@ -65,7 +65,7 @@ export function createRouter () {
routes: [
{
path: "/",
component: _0232240e,
component: _62267d94,
name: "index"
}
],
Expand Down
35 changes: 2 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pm2 startOrReload pm2.json
- [ThinkJS](https://thinkjs.org/)
- [Nuxt.js](https://nuxtjs.org/guide/)
- [Vue.js](http://vuejs.org/guide/)
- [thin-nuxt](https://github.com/baisheng/think-nuxt)

## Licenses

Expand All @@ -46,36 +47,4 @@ pm2 startOrReload pm2.json


## Tips
### 非 nuxt 资源的访问
如果希望某些 controller 如 api 不被 nuxt 中间件拦截,请参考下面的代码修改 middleware
```javascript
module.exports = options => {
let err = null
const middleware = async (ctx, next) => {
// 处理不被拦截的路径
let subdomain = ctx.url.split('/')[1]
if (subdomain === 'api') {
return next()
} else {
// let isApi
// 默认访问状态为 404
ctx.status = 200
ctx.req.session = await ctx.session()
await nuxt.render(ctx.req, ctx.res)
const startTime = Date.now();
return next().catch(e => {
err = e
}).then(() => {
const endTime = Date.now();
// nuxt.render(ctx.req, ctx.res)
if (err) return Promise.reject(err); // 如果后续执行逻辑有错误,则将错误返回
return new Promise((resolve, reject) => {
// console.log(`request exec time: ${endTime - startTime}ms`);
})
}
)
}
}
return middleware
}
```

File renamed without changes.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"think-logger3": "^1.0.0",
"think-model": "^1.0.0",
"think-model-mysql": "^1.0.0",
"think-nuxt": "^1.0.0",
"think-session": "^1.0.0",
"think-session-file": "^1.0.5",
"think-view": "^1.0.0",
Expand Down
11 changes: 8 additions & 3 deletions src/config/middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require('path');
const config = require(path.join(think.ROOT_PATH, '/nuxt.js'))
const isDev = think.env === 'development';
const nuxt = require('../middleware/nuxt')
const nuxt = require('think-nuxt')

module.exports = [
{
Expand Down Expand Up @@ -34,8 +35,12 @@ module.exports = [
options: {}
},
{
handle: 'nuxt',
options: {}
handle: nuxt,
options: {
config: config,
unless: [/^\/api?/],
isDev: isDev
}
},
'logic',
'controller'
Expand Down
File renamed without changes.
36 changes: 0 additions & 36 deletions src/middleware/nuxt.js

This file was deleted.

File renamed without changes.
91 changes: 91 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5391,6 +5391,73 @@ nuxt@^1.4.0:
webpack-hot-middleware "^2.21.0"
webpack-node-externals "^1.6.0"

nuxt@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/nuxt/-/nuxt-1.4.1.tgz#78d268c2787b372d835511784a6af2a02ad4c5f3"
dependencies:
"@nuxtjs/youch" "^4.2.3"
ansi-html "^0.0.7"
autoprefixer "^7.2.5"
babel-core "^6.26.0"
babel-loader "^7.1.2"
babel-preset-vue-app "^2.0.0"
caniuse-lite "^1.0.30000808"
chalk "^2.3.1"
chokidar "^2.0.1"
clone "^2.1.1"
compression "^1.7.1"
connect "^3.6.5"
css-hot-loader "^1.3.7"
css-loader "^0.28.9"
debug "^3.1.0"
es6-promise "^4.2.4"
etag "^1.8.1"
extract-text-webpack-plugin "^3.0.2"
file-loader "^1.1.6"
fresh "^0.5.2"
friendly-errors-webpack-plugin "^1.6.1"
fs-extra "^5.0.0"
glob "^7.1.2"
hash-sum "^1.0.2"
html-minifier "^3.5.9"
html-webpack-plugin "^2.30.1"
launch-editor "^2.2.1"
launch-editor-middleware "^2.2.1"
lodash "^4.17.5"
lru-cache "^4.1.1"
memory-fs "^0.4.1"
minimist "^1.2.0"
opencollective "^1.0.3"
postcss "^6.0.17"
postcss-cssnext "^3.1.0"
postcss-import "^11.1.0"
postcss-import-resolver "^1.1.0"
postcss-loader "^2.1.0"
postcss-url "^7.3.0"
pretty-error "^2.1.1"
progress-bar-webpack-plugin "^1.10.0"
semver "^5.5.0"
serialize-javascript "^1.4.0"
serve-static "^1.13.2"
server-destroy "^1.0.1"
source-map "^0.7.0"
style-resources-loader "^1.0.0"
uglifyjs-webpack-plugin "^1.1.8"
upath "^1.0.2"
url-loader "^0.6.2"
vue "^2.5.13"
vue-loader "^13.7.2"
vue-meta "^1.4.3"
vue-router "^3.0.1"
vue-server-renderer "^2.5.13"
vue-template-compiler "^2.5.13"
vuex "^3.0.1"
webpack "^3.11.0"
webpack-bundle-analyzer "^2.10.0"
webpack-dev-middleware "^2.0.5"
webpack-hot-middleware "^2.21.0"
webpack-node-externals "^1.6.0"

nyc@^7.0.0:
version "7.1.0"
resolved "http://registry.npm.taobao.org/nyc/download/nyc-7.1.0.tgz#8e14971f3a15d1abbec7ac610ef54cb889e9ffb4"
Expand Down Expand Up @@ -7881,6 +7948,12 @@ think-mysql@^1.0.2:
think-helper "^1.0.5"
think-instance "^1.0.0"

think-nuxt@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/think-nuxt/-/think-nuxt-1.0.0.tgz#82528a546a8e3c247a5bd8fe6ee5826810992714"
dependencies:
nuxt "^1.4.1"

think-payload@^1.0.0:
version "1.3.2"
resolved "http://registry.npm.taobao.org/think-payload/download/think-payload-1.3.2.tgz#2517e9e1c737f77480b7e9d85525f2d6ca6424dc"
Expand Down Expand Up @@ -8431,6 +8504,24 @@ vue-loader@13.7.0:
vue-style-loader "^3.0.0"
vue-template-es2015-compiler "^1.6.0"

vue-loader@^13.7.2:
version "13.7.2"
resolved "https://registry.yarnpkg.com/vue-loader/-/vue-loader-13.7.2.tgz#f633f6393e905ffd730400f03c99353618c08fe8"
dependencies:
consolidate "^0.14.0"
hash-sum "^1.0.2"
loader-utils "^1.1.0"
lru-cache "^4.1.1"
postcss "^6.0.8"
postcss-load-config "^1.1.0"
postcss-selector-parser "^2.0.0"
prettier "^1.7.0"
resolve "^1.4.0"
source-map "^0.6.1"
vue-hot-reload-api "^2.2.0"
vue-style-loader "^3.0.0"
vue-template-es2015-compiler "^1.6.0"

vue-meta@^1.4.3:
version "1.5.0"
resolved "http://registry.npm.taobao.org/vue-meta/download/vue-meta-1.5.0.tgz#02a9f166a1e87763a87bfd47431122ed575bcdde"
Expand Down

0 comments on commit a84e747

Please sign in to comment.