├── client ├── assets │ ├── img │ │ └── logo.png │ └── css │ │ └── main.css ├── static │ └── favicon.ico ├── middleware │ └── auth.js ├── components │ └── Footer.vue ├── store │ ├── index.js │ ├── README.md │ └── about.js ├── app.html ├── layouts │ ├── error.vue │ └── default.vue └── pages │ ├── index.vue │ └── about.vue ├── backpack.config.js ├── config ├── app.alp.js ├── app.dev.js ├── app.pro.js ├── app.uat.js └── log4js.js ├── .gitignore ├── .eslintrc.js ├── server ├── controllers │ ├── error.js │ └── user.js ├── autoRoutes.js └── index.js ├── README.md ├── nuxt.config.js ├── package.json └── helper ├── services.js └── utils.js /client/assets/img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1peng/vue2_koa2_nuxt_ssr/HEAD/client/assets/img/logo.png -------------------------------------------------------------------------------- /client/static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/1peng/vue2_koa2_nuxt_ssr/HEAD/client/static/favicon.ico -------------------------------------------------------------------------------- /backpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | webpack: (config, options, webpack) => { 3 | config.entry.main = './server/index.js' 4 | return config 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /client/middleware/auth.js: -------------------------------------------------------------------------------- 1 | export default function (context) { 2 | context.userAgent = context.isServer ? context.req.headers['user-agent'] : navigator.userAgent 3 | } 4 | -------------------------------------------------------------------------------- /client/components/Footer.vue: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /config/app.alp.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | apiPath: { 3 | local: { 4 | host: 'http://localhost:3000', 5 | getUserList: { 6 | method: 'get', 7 | url: '/user/list' 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /config/app.dev.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | apiPath: { 3 | local: { 4 | host: 'http://localhost:3000', 5 | getUserList: { 6 | method: 'get', 7 | url: '/user/list' 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /config/app.pro.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | apiPath: { 3 | local: { 4 | host: 'http://localhost:3000', 5 | getUserList: { 6 | method: 'get', 7 | url: '/user/list' 8 | } 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /config/app.uat.js: -------------------------------------------------------------------------------- 1 | console.log('uat') 2 | module.exports = { 3 | apiPath: { 4 | local: { 5 | host: 'http://localhost:3000', 6 | getUserList: { 7 | method: 'get', 8 | url: '/user/list' 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | node_modules 3 | 4 | # logs 5 | npm-debug.log 6 | 7 | # Nuxt build 8 | .nuxt 9 | 10 | # Nuxt generate 11 | dist 12 | 13 | # build 14 | build 15 | 16 | # .idea 17 | .idea 18 | 19 | # server/logs 20 | server/logs 21 | 22 | # config/app.js 23 | config/app.js 24 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: 'babel-eslint', 4 | env: { 5 | browser: true, 6 | node: true 7 | }, 8 | extends: 'standard', 9 | // required to lint *.vue files 10 | plugins: [ 11 | 'html' 12 | ], 13 | // add your custom rules here 14 | rules: {}, 15 | globals: {} 16 | } 17 | -------------------------------------------------------------------------------- /client/store/index.js: -------------------------------------------------------------------------------- 1 | export const state = () => ({ 2 | counter: 0 3 | }) 4 | 5 | export const getters = { 6 | counter (state) { 7 | return state.counter 8 | } 9 | } 10 | 11 | export const mutations = { 12 | increment (state) { 13 | state.counter++ 14 | }, 15 | reduction (state) { 16 | state.counter-- 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /server/controllers/error.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by wangyipeng on 2017/9/27. 3 | */ 4 | const router = require('koa-router')() 5 | router.get('/reporter', async function (ctx, next) { 6 | let logger = ctx.Log4js.getLogger('reporter') 7 | logger.error(ctx.query.err) 8 | ctx.body = {result: 'ok'} 9 | }) 10 | 11 | module.exports = router 12 | -------------------------------------------------------------------------------- /client/store/README.md: -------------------------------------------------------------------------------- 1 | # STORE 2 | 3 | This directory contains your Vuex Store files. 4 | Vuex Store option is implemented in the Nuxt.js framework. 5 | Creating a index.js file in this directory activate the option in the framework automatically. 6 | 7 | More information about the usage of this directory in the documentation: 8 | https://nuxtjs.org/guide/vuex-store 9 | 10 | **This directory is not required, you can delete it if you don't want to use it.** 11 | -------------------------------------------------------------------------------- /server/controllers/user.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by wangyipeng on 2017/9/27. 3 | */ 4 | import services from '../../helper/services' 5 | const router = require('koa-router')() 6 | router.get('/index', async function (ctx, next) { 7 | let advList = await services.local.getUserList() 8 | ctx.body = advList 9 | }) 10 | 11 | router.get('/list', async function (ctx, next) { 12 | ctx.body = [ 13 | { 14 | name: 'yipeng', age: '29' 15 | }, 16 | { 17 | name: 'yihang', age: '18' 18 | } 19 | ] 20 | }) 21 | 22 | module.exports = router 23 | -------------------------------------------------------------------------------- /client/assets/css/main.css: -------------------------------------------------------------------------------- 1 | html, body 2 | { 3 | background-color: #fff; 4 | color: #2e2f30; 5 | letter-spacing: 0.5px; 6 | font-family: "Source Sans Pro", Arial, sans-serif; 7 | height: 100vh; 8 | margin: 0; 9 | } 10 | 11 | footer 12 | { 13 | padding: 20px; 14 | text-align: center; 15 | border-top: 1px solid #ddd; 16 | } 17 | 18 | a, a:hover, a:focus, a:visited 19 | { 20 | color: #41B883; 21 | } 22 | 23 | .logo { 24 | width: 243px; 25 | height: 218px; 26 | } 27 | 28 | .page-enter-active, .page-leave-active 29 | { 30 | transition: opacity .5s 31 | } 32 | .page-enter, .page-leave-active 33 | { 34 | opacity: 0 35 | } 36 | -------------------------------------------------------------------------------- /client/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | {{ HEAD }} 5 | 18 | 19 | 20 | {{ APP }} 21 | 22 | -------------------------------------------------------------------------------- /client/store/about.js: -------------------------------------------------------------------------------- 1 | export const state = () => ({ 2 | abouts: [ 3 | { 4 | text: 'aaaa11', 5 | done: false 6 | }, 7 | { 8 | text: 'aaaa2222', 9 | done: false 10 | }, 11 | { 12 | text: '222221119999999922', 13 | done: false 14 | } 15 | ] 16 | }) 17 | export const getters = { 18 | abouts (state) { 19 | return state.abouts 20 | } 21 | } 22 | 23 | export const mutations = { 24 | add (state, text) { 25 | state.abouts.push({ 26 | text: text, 27 | done: false 28 | }) 29 | }, 30 | remove (state, { todo }) { 31 | state.abouts.splice(state.abouts.indexOf(todo), 1) 32 | }, 33 | toggle (state, todo) { 34 | todo.done = !todo.done 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue2_koa2_nuxt_ssr 2 | 3 | > A vue project with koa2 and nuxt for ssr. 4 | 5 | ## Build Setup 6 | 7 | ``` bash 8 | # install dependencies 9 | $ npm install # Or yarn install*[see note below] 10 | 11 | # serve with hot reload at localhost:3000 12 | $ npm run dev 13 | 14 | # build for production and launch server 15 | $ npm run build 16 | $ npm start 17 | 18 | # generate static project 19 | $ npm run generate 20 | ``` 21 | 22 | *Note: Due to a bug in yarn's engine version detection code if you are 23 | using a prerelease version of Node (i.e. v7.6.0-rc.1) you will need to either: 24 | 1. Use `npm install` 25 | 2. Run `yarn` with a standard release of Node and then switch back 26 | 27 | For detailed explanation on how things work, checkout the [Nuxt.js docs](https://github.com/nuxt/nuxt.js). 28 | -------------------------------------------------------------------------------- /config/log4js.js: -------------------------------------------------------------------------------- 1 | export default { 2 | 'appenders': [ 3 | { 4 | 'type': 'console' 5 | }, 6 | { 7 | 'type': 'clustered', 8 | 'appenders': [ 9 | { 10 | 'type': 'dateFile', 11 | 'filename': 'http.log', 12 | 'pattern': '-yyyy-MM-dd', 13 | 'category': 'http' 14 | }, 15 | { 16 | 'type': 'file', 17 | 'filename': 'app.log', 18 | 'maxLogSize': 10485760, 19 | 'pattern': '-yyyy-MM-dd', 20 | 'numBackups': 5 21 | }, 22 | { 23 | 'type': 'logLevelFilter', 24 | 'level': 'ERROR', 25 | 'appender': { 26 | 'type': 'file', 27 | 'filename': 'errors.log' 28 | } 29 | } 30 | ] 31 | } 32 | ] 33 | } 34 | -------------------------------------------------------------------------------- /server/autoRoutes.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by wangyipeng on 2017/9/26. 3 | */ 4 | const router = require('koa-router')() 5 | const path = require('path') 6 | const fs = require('fs') 7 | var exports = {} 8 | exports['auto'] = function (app) { 9 | let files = fs.readdirSync(path.join(__dirname, 'controllers')) 10 | 11 | let jsFiles = files.filter((f) => { 12 | return f.endsWith('.js') 13 | }, files) 14 | 15 | // 控制器文件 16 | for (let f of jsFiles) { 17 | console.log(`import controller from file ${f}...`) 18 | let name = f.substring(0, f.length - 3) 19 | exports[name] = require('./controllers/' + f) 20 | router.use('/' + name, exports[name].routes(), exports[name].allowedMethods()) 21 | app.use(exports[name].routes(), exports[name].allowedMethods()) 22 | } 23 | } 24 | module.exports = exports 25 | -------------------------------------------------------------------------------- /client/layouts/error.vue: -------------------------------------------------------------------------------- 1 | 2 |
4 |
4 |
4 |