我想擴展快速模塊,並添加到他的我的方法。 爲了這個,我用的util模塊 我的一段代碼:如何使用util將方法添加到模塊express()中?
var expressApplication = require('express');
var util = require('util');
function Application() {
expressApplication.apply(this, arguments);
this.handlers = {};
}
util.inherits(Application, expressApplication);
//add my method
Application.prototype.write = function(data) {
console.log(data)
};
然後我把它叫做:
var app = new Application();
//check inheritance
console.log(app instanceof expressApplication); // true
console.log(Application.super_ === expressApplication); // true
//and check the work my method
console.log(app.write('test'));
console.log(app.use) /// ---> error!!!
但是,當我想打電話給快遞的方法 - 這是不提供給我,console.log(app.use)
有錯誤app.use is not a function
請告訴我什麼是錯的,在哪裏可以解決問題?另外,相反,我試鏡明確koa,有練習。
謝謝!
你忘了傳遞一些東西來使用()? http://expressjs.com/en/4x/api.html#app.use – Cruiser
不,我沒忘記()。如果輸出到控制檯'console.log(app.write)',他返回'[Function]'。但'console.log(app.use)'返回'undefined'。沒有'express'的繼承,我不明白爲什麼 – ennet