Friday 19 May 2017

Difference between require('module')() and const mod = require('module') mod() in node/express

I have two files: server.js and db.js

server.js looks as such:

...
const app = express();

app.use('/db', db());

app.listen(3000, () => {
  console.log('Server started on port 3000')
});
...

and db.js as such:

...
function init() {
  const db = require('express-pouchdb')(PouchDB, {
    mode: 'minimumForPouchDB'
  });

  return db;
}
...

This works just fine, and I am able to reach the pouchdb http-api from my frontend. But before, I had const PouchDBExpress = require('pouchdb-express'); in the top of db.js, and the first line in init() looked like this; const db = PouchDBExpress(PouchDB, {. This gave an error in one of the internal files in pouchdb saying cannot set property query on req which only has getters (paraphrasing).

So this made me copy the exaples from pouchdb-servers GitHub examples which requires and invokes pouched-express directly, and everthing worked fine. Is there an explanation for this? I'm glad it works now, but I'm sort of confused as to what could cause this.



via Christer Olsen

No comments:

Post a Comment