1

I have a node application and I took the following functionality and put it in separate file in new folder as a new module. In this file I need to handle some action like save delete edit etc. I have two questions:

  1. Should I separate the functionality inside this file to actions and expose it differently?

  2. In any case how should I call to this functionality with the parameters which is needed to all the actions like req, res, path?

I'm looking for concrete examples.

This is the code that I use:

module.exports = function () {
  const fs = require('fs')

  function fileAction (req, res, urlAction) {
    switch (urlAction) {

      case 'save':                  
        const writeStream = fs.createWriteStream('c://myfile.txt', { flags: 'w' })
        req.pipe(writeStream)
        req.on('end', function () {
          console.log('Finish to update data file')
        })
        res.end()
        break

      case 'delete':
      case 'update':
      default:
    }
}
kube
  • 13,176
  • 9
  • 34
  • 38

2 Answers2

3

I like this approach more than implementing functions inside and export lexical scope.

Simply with this approach I feel like the concern of "exporting" is separated from the implementation of the functions. In addition to that you can rename the functions you are going to export. Most importantly you might control better what you want and do not want to export.

var delete= function(){

};

var update = function(){

};

var save = function(){

};

module.exports.update = update;
module.exports.delete = delete;
module.exports.save = save;

Then you'll be able to call methods from your main file:

var file = require('./file.js');
file.save();
file.delete();
file.update();
ralzaul
  • 4,280
  • 6
  • 32
  • 51
  • 1
    This is the Revealing Module Pattern. https://carldanley.com/js-revealing-module-pattern/ – kube Jun 25 '15 at 20:10
2

You should do something more object-oriented:

module.exports = {

    save: function () {

    },

    delete: function () {

    },

    update: function () {

    }
}

Then you'll be able to call methods from your main file:

const FileLib = require('./fileLib.js')

FileLib.save()

If you plan to use this as logic inside an Express application, you do not really need to use req and res directly from inside your module except if you are writing an Express middleware or a router.

But what I would recommend you is to use your library from the router:

const FileLib = require('./fileLib.js')

router.put('/file/:id', function (req, res) {

    // Do your stuff with your library
    FileLib.save(req.param('fileToSave'))

    res.send()
})

Your library should not be too coupled to the express architecture unless it's a middleware.

Writing RESTful Express routing might also be a good idea. Use HTTP verbs to specify your action to the API.

kube
  • 13,176
  • 9
  • 34
  • 38
  • Thanks Kube,Voted up.I've one question regard the using of express,currenlty I use the http-proxy module to create servers as part of my app which is working OK,now when you hit the browser/postman the action (req,res) is coming to createServer function and then I route the call to the new file,can I use the express also in this case ? –  Jun 24 '15 at 20:26
  • I use this module:https://github.com/nodejitsu/node-http-proxy and there option Setup a stand-alone proxy server with custom server logic... please suggest if Its ok to use also express and how? –  Jun 24 '15 at 20:27
  • I can't tell you I never used node-http-proxy but it seems ok: http://stackoverflow.com/questions/20431697/node-http-proxy-and-express You just pipe the request to another module, and it seems ok in term of architecture. But even if interesting it seems really overkill to me, are you planning to make load balancing on your application ? – kube Jun 24 '15 at 20:41