node.js - nodejs 的sails 框架如何修改ejs的后綴為html
問題描述
問題解答
回答1:已找到解決方案當我們將模板的后綴都改為html的時候,訪問頁面發現了報錯,很明顯是后綴配置不正確。
{ 'message': 'Could not render view 'index'. Tried locating view file @ '/Users/sun.huajie/Documents/Workspace/node_sails/myapp/views/index'. Layout configured as 'layout', so tried using layout @ '/Users/sun.huajie/Documents/Workspace/node_sails/myapp/views/layout')', 'code': 'E_VIEW_FAILED', 'status': 500, 'view': { 'name': 'index', 'root': '/Users/sun.huajie/Documents/Workspace/node_sails/myapp/views', 'defaultEngine': 'ejs', 'ext': '.ejs' }}
我去查找了Could not render view這段代碼,發現多出來一個節點view,這時候我尋找一個關鍵字defaultEngine來進行了全文搜索,找到了一處定位。
// view if (!view) { view = new (this.get(’view’))(name, { defaultEngine: this.get(’view engine’), root: this.get(’views’), engines: engines }); . . .
按照上面的代碼來看,說明default Engine是從view engine的配置中獲取的。那我們再繼續搜索view engine。找到如下代碼:
// Configure views if hook enabled if (sails.hooks.views) {// TODO: explore handling this differently to avoid potential// timing issues with view engine configurationsails.after(’hook:views:loaded’, function() { var View = require(’./view’); // Use View subclass to allow case-insensitive view lookups expressApp.set(’view’, View); // Set up location of server-side views and their engine expressApp.set(’views’, sails.config.paths.views); // Teach Express how to render templates w/ our configured view extension expressApp.engine(sails.config.views.engine.ext, sails.config.views.engine.fn); // Set default view engine sails.log.verbose(’Setting default Express view engine to ’ + sails.config.views.engine.ext + ’...’); expressApp.set(’view engine’, sails.config.views.engine.ext);});
通過上面的代碼我們可以看出 這個view engine實際上是來自sails.config.view.engine.ext的配置,跟我們的配置文件路徑一樣。
/Users/sun/Documents/Workspace/node_sails/myapp/config/view.js
這個時候我們在這代碼里打一個斷點
console.info(sails.config);
然后奇妙的事情出現了
... views: { engine: { name: ’ejs’, ext: ’ejs’, fn: [Function] }, layout: ’layout’, partials: false },...
看來這就是對應的配置,但是當我打開這個文件的時候發現里面的engine是這樣定義的
engine:’ejs’
wtf!,這是什么情況,為什么格式不對。我以為會是json的字符串。
然后大膽的來調整一下吧
engine: { name:’ejs’, ext:’html’ },
再次輸出就得到了正確的配置改變
{ engine: { name: ’ejs’, ext: ’html’, fn: [Function] },
這個時候我們滿懷信心重啟后刷新頁面,卻發現了另外一個報錯!!!!wtf
Error: ENOENT: no such file or directory, open ’/Users/sun.huajie/Documents/Workspace/node_sails/myapp/views/header.ejs’ at Error (native) at Object.fs.openSync (fs.js:640:18) at fs.readFileSync (fs.js:508:33) at Object.exports.parse (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:160:19) at exports.compile (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:225:15) at Object.exports.render (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:285:10) at Object.exports.renderFile (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:319:20) at module.exports (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/index.js:85:7) at /usr/local/lib/node_modules/sails/node_modules/ejs-locals/index.js:131:7 at Object.exports.renderFile (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/node_modules/ejs/lib/ejs.js:319:3) at SailsView.module.exports [as engine] (/usr/local/lib/node_modules/sails/node_modules/ejs-locals/index.js:85:7) at SailsView.View.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/view.js:76:8) at Function.app.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/application.js:565:10) at ServerResponse.res.render (/usr/local/lib/node_modules/sails/node_modules/express/lib/response.js:845:7) at ServerResponse.res.view (/usr/local/lib/node_modules/sails/lib/hooks/views/res.view.js:284:16) at Object.index (/Users/sun.huajie/Documents/Workspace/node_sails/myapp/api/controllers/UserController.js:36:14) at wrapper (/usr/local/lib/node_modules/sails/node_modules/@sailshq/lodash/lib/index.js:3250:19) at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:181:5) at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:135:11) at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
報錯發生了變化,說明剛剛的修改肯定是有作用的。然后這個時候我單獨把header的后綴名改回ejs發現,前端頁面正常了。看來這個header文件加載的時候,后綴名是程序里面寫好的了。
剩下的流程就是判斷到底哪兒的問題了,根據報錯的執行順序來找下到底那兒的問題!
if (0 == js.trim().indexOf(’include’)) {var name = js.trim().slice(7).trim();if (!filename) throw new Error(’filename option is required for includes’);var path = resolveInclude(name, filename);include = read(path, ’utf8’);include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });buf += '’ + (function(){' + include + '})() + ’';js = ’’; }
定位到上面的代碼,發現是path變量除出了問題,導致沒有找到。我們打印一下name和filename
header/Users/sun.huajie/Documents/Workspace/node_sails/myapp/views/layout.html
這個時候我們定位到了方法resolveInclude
function resolveInclude(name, filename) { var path = join(dirname(filename), name); var ext = extname(name); if (!ext) path += ’.ejs’; return path;}
看到了吧,說明這個地方是進行拼接了,但是只有在extname返回為空的情況下才會拼接,說明這里的name傳遞進來的時候是沒有攜帶后綴名的!!!!
所以看到這里基本上明白了,在模板layout.html中,使用了include命令來加載了header
<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'> <% include header %>
所以我們將這里的header改為header.html然后再重新訪問,就正常了。
回答2:那你為什么不試試jade模板了,可以直接用原生html寫
相關文章:
1. python - 有什么好的可以收集貨幣基金的資源?2. docker容器呢SSH為什么連不通呢?3. docker鏡像push報錯4. docker安裝后出現Cannot connect to the Docker daemon.5. docker start -a dockername 老是卡住,什么情況?6. docker網絡端口映射,沒有方便點的操作方法么?7. node.js - nodejs debug問題8. MySQL數據庫中文亂碼的原因9. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題10. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””
