E
E
ennet2016-03-17 14:03:40
JavaScript
ennet, 2016-03-17 14:03:40

How to add your own method to the Express() module using util?

Hello. I'm trying to extend the module expressand add my method there.
In general, the util module was used for this.
My piece of code:

var expressApplication = require('express');
var util = require('util');

function Application() {
    expressApplication.apply(this, arguments);
    this.handlers = {};
}
util.inherits(Application, expressApplication);

//добавление своего метода
Application.prototype.write = function(data) {
    console.log(data)
};

Then I call it:
var app = new Application();

//проверяем наследование
console.log(app instanceof expressApplication); // true
console.log(Application.super_ === expressApplication); // true

//проверяем мои методы
console.log(app.write('test'));

But when I want to call the methods of the express itself, they are not available to me, for example, it console.log(app.use)says this with an error, app.use is not a function
please tell me what is wrong, where to look to fix the problem? As an option, I tried it instead of express koa, it worked there.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TOYS, 2016-03-24
@TOYS

var express = require('express');
var proto = express.application;

proto.write = function (msg) {
   console.log(msg);
};

var app = express();

console.log(app.write); // [Function]

app.write('test!');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question