1

I have the following code in node.js, using express:

app.post('/printUp', function(req, res) {
    var multipl = new require('./multUploud').printNum(req, res);
})

and i have the following in my multUploud.js:

var partNum = 0;
module.exports = {
     printNum: function (req, res){
        partNum ++;
        console.log(partNum);
    }
}

If I sent two commands of post, I see that partNum is 1 in the first iteration, and 2 in the second.

is there any option to create a new instance for every app.post request, so it prints 1 and 1?

1
  • What's the new doing there? Commented Apr 28, 2014 at 15:16

1 Answer 1

2

Just make partNum a property of your object. Your multUploud.js should look somewhat like that:

module.exports = MultiUpload;

function MultiUpload() {
  this.partNum = 0;
}

MultiUpload.prototype.printNum = function (req, res) {
  this.partNum++;
  console.log(this.partNum);
}

Then, in your Express post:

app.post('/printUp', function(req, res) {
    var MultiUpload = require('./multUploud');
    var multipl = new MultiUpload();
    multipl.printNum(req, res);
})
Sign up to request clarification or add additional context in comments.

4 Comments

and the call to the method is the same?
Yes, everything else is the same.
It writes the there is no method printNum for this object
Oh, there's a pair of braces missing: var multipl = new require('./multUploud')().printNum(req, res);. I updated the answer with a cleaner version of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.