js 模版加載到前端javascript
簡單有效不高端前端
配個路由 /js/:filename , 用 readTemplate 響應請求,此處響應請求的服務器是node後端 koa1,前端能夠按模塊方式直接 require 模板java
'use strict' var Fs = require('fs'); function* readTemplate() { var fileName = this.params.fileName; fileName = fileName.replace(/[-_]/g,'/') var dir = 'public/template/'; var content = Fs.readFileSync(dir+fileName, 'utf-8'); content = content.replace(/[\r\n]+/g," "); content = content.replace(/(['"])/g,"\\$1") content = 'define(function(){ return "'+content+'" })'; this.body = content; } module.exports = { readTemplate:readTemplate }