简体   繁体   English

Node.js:创建具有动作分离的模块

[英]Node.js: create module with separation of actions

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 ? 无论如何,我应该如何使用所有操作(例如reqrespath )所需的参数调用此功能?

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:
    }
}

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();

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. 如果您打算在Express应用程序中将其用作逻辑,则实际上不需要直接在模块内部使用reqres ,除非您正在编写Express中间件或路由器。

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. 编写RESTful Express路由也可能是一个好主意。 Use HTTP verbs to specify your action to the API. 使用HTTP动词来指定对API的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM