1.一個應用使用兩個模版引擎 ,需要注意的是,
i.express要求設置默認模版引擎,不然會報錯
ii.使用的時候要加上模版后綴
var express = require('express')
var engines = require('consolidate')
...
app.set('view engine', 'pug')
app.engine('pug', engines.pug);
app.engine('ejs', engines.ejs);
...
res.render('login1.ejs', { message: req.flash('loginMessage') })
2.npm list 查看所安裝的包以及相應的版本號
3.對一些全局的模塊只有在特定的請求進行單獨加載 寫一個中間件 ,使用正則進行請求路徑匹配,在res中加上對應的方法。
app.use('/youlun/:cityLetter', function(req, res, next){
res.locals = {delimiter: '?'};
next();
})
4.看一個模塊例如express,先看package.json找main找主入口,沒有main字段就找index.js
5.有問題看不懂就去看源碼。
6.http://stackoverflow.com/questions/7591240/what-does-dot-slash-refer-to-in-terms-of-an-html-file-path-location
/ means the root of the current drive;
./ means the current directory;
../ means the parent of the current directory
7.碰到一個奇怪的問題,就是express明明設置了app.use(express.static('../../public'));
,但是node modules/ares/app.js
啟動后,訪問不到相應的public文件。解決思路:
1.node_modules中找express模塊包,在其中找package.json,沒有main字段,找index.js
2.再找./lib/express,在其中找到了exports.static = require('serve-static')
3.在express模塊包中 找到相應的node_modules文件夾下的 server-static模塊
4.找到module.exports = serveStatic
因為只傳了一個路徑參數,在這里找和 root參數相關的即可 ,找到了
opts.root = resolve(root)
。path.resolve6.看到這里問題就明朗了,因為此時express會把靜態路徑轉成相當于 modules目錄下的../../public 而不是 app.js的。
總結 以后express傳路徑還是傳絕對路徑,多看源碼: )
9.新建多層目錄 一定要一層一層的建,同時注意權限問題,有權限的話加上sudo,
10.require() 當前目錄引用當前目錄或者當前文件夾的文件
require('config/config.js') //提示找不到模塊,
require('./config/config.js') //正常
//不用用html的引用思維去思考require的功能,html的引用是http請求 ,require是文件目錄請求