node.js - Is it possible to send data directly to a constructor? -


first time using node.js, mongodb, express web-development.

say have constructor 'createobject' in file separate routing or driver file.

can use module.exports.createobject, , require , create object within driver, can call single function , have database insert data directly object me when call in routing file?

(i.e. 'when user visits page, import createobject constructor, db.insert(createobject object, data), export information our display functions)

does export work that?

i'm trying avoid routing every file display functions adding each entry array './:collection/:item'.

express doesn't care constructors or objects. can use them, need plumbing use them within middleware function. instead, might want think in terms of middleware functions.

a common pattern in node development define 1 (or more) middleware functions in other files , require app. can chain arbitrary number of middleware functions , attach them route. this:

// app.js var dbinserter = require('./mydbinserter.js'); app.use('/:collection/:item', dbinserter, othermiddleware ... );  // mydbinserter.js module.exports = function(req,res,next) {   var collectionname = req.params.collection;   var itemid = req.params.item;   // create object, insert db, etc.   next(); } 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -