├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── app_1.js ├── dist ├── avalon.js ├── avalon2.2.js ├── serverRender.js ├── serverRender2.2.js └── vm.js ├── index.js ├── package.json ├── src ├── aaa.html └── vm.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Coverage directory used by tools like istanbul 12 | coverage 13 | 14 | # node-waf configuration 15 | .lock-wscript 16 | 17 | # Dependency directory 18 | node_modules 19 | 20 | # Optional npm cache directory 21 | .npm 22 | 23 | # Optional REPL history 24 | .node_repl_history 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 司徒正美 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # avalon-server-render-example 2 | avalon2+koa2的后端渲染例子 3 | 4 | npm install 5 | 6 | npm start 7 | 8 | 9 | ------------ 10 | 后端渲染的流程 11 | 12 | 1. 引入最新版 [avalon](https://github.com/RubyLouvre/avalon/tree/master/dist) 这里用avalon.modern体积少些 13 | 2. 引入avalon仓库下的serve下的文件[serveRender.js](https://github.com/RubyLouvre/avalon/tree/master/src/server) 14 | 3. 引入你定义VM的文件 (所有DOM操作要在回调里进行,不要出现 window, document, 方便能在nodejs环境中运行) 15 | 对你的VM使用webpack进行打包 (目的是处理module.exports, require) 16 | 4. 引入你该页面的模板 17 | 5. 将VM与模板放进serveRender方法,得到一个对象,里面包含渲染好的HTML(**A**) 及 一个包括所有模板的对象(**B**) 18 | 6. 创建一个script标签, 里面定义一个`avalon.serverTemplates`对象, 将**B**对象赋给它 19 | 7. 将上面的标签与A页面, 赋给ctx.body发往前端 20 | 21 | 22 | ------ 23 | 24 | ```javascript 25 | //1. 引入avalon 26 | var vm = require('./src/avalon') 27 | //2. 引入avalon的后端渲染器 28 | var serveRender = require('./dist/serverRender') 29 | //3. 当前页面VM 30 | var vm = require('./src/vm') 31 | //4. 当前页面模板 32 | var test = fs.readFileSync('./src/aaa.html', 'utf-8'); 33 | 34 | //5. 35 | var obj = serveRender(vm, test) 36 | 37 | //6. 38 | var files = JSON.stringify(obj.templates) 39 | var script = '