js 模版加载到前端
js 模版加载到前端
简单有效不高端
配个路由 /js/:filename , 用 readTemplate 响应请求,此处响应请求的服务器是node后端 koa1,前端可以按模块方式直接 require 模板
'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 }