I have a problem with Node.js and module.exports. I understand that module.exportsthis is a call to return an object, this object has any properties that are assigned to it.
If I have a file structure like this:
function Format(text) {
this.text = text;
}
module.exports = Format;
with this:
var formatting = require('./formatting');
Is there a way to initialize an object Formatand use it like this:
formatting('foo');
console.log(formatting.text);
Whenever I try to do this, I get an error formatting is not a function. Then I have to do it like this:
var x = new formatting('foo');
console.log(x.text);
which seems bulky.
The modules such as keypressand requestthey can be used directly from the gate, for example:
var keypress = require('keypress');
keypress(std.in);
or
var request = require('request);
request('http:
if (!error && response.statusCode == 200) {
console.log(body)
}
})
How it works?
source
share