javascript - How to add a method to module express() with using util? -


i trying expand express module, , add method. use util module piece of code:

var expressapplication = require('express'); var util = require('util');  function application() {     expressapplication.apply(this, arguments);     this.handlers = {}; } util.inherits(application, expressapplication);  //add method application.prototype.write = function(data) {     console.log(data) }; 

then call it:

var app = new application();  //check inheritance console.log(app instanceof expressapplication); // true console.log(application.super_ === expressapplication); // true  //and check work method console.log(app.write('test'));   console.log(app.use) /// ---> error!!! 

but when want call method of express - not available me,console.log(app.use) has error app.use not function

please tell me wrong, fix problem? alternatively, instead auditioned express koa, there practiced.

thanks!

the function exported require('express') merely factory function own prototype mixing, there isn't 1 object inherit from.

you have couple of options:

  • copy express's factory function uses own express.application subclass
  • directly modify original express.application

the benefit of first 1 don't affect express app instances, typically you'd want. writing prototypes of third party modules (or built-in objects matter) frowned upon , can cause problems (especially if end users attach own properties same names).


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -