D
D
Dimon Durak2015-10-03 15:40:46
Node.js
Dimon Durak, 2015-10-03 15:40:46

How to properly connect and use external modules in ImpressAS?

I figured out how workers work , but now I can’t figure out how to call modules from node_modules from a worker.
This is what I currently have: get.js
file :

module.exports = function(client, callback) {
  if (client.query.mode == 'upload') {client.fork('upload')};
  callback();
};

File upload.js :
I first ran this code separately with my hands and it worked. When porting to Impress, I replaced only the declarations of the fs and expat variables (it was through require, it became through IAS).
module.exports = function(client, callback) {
//  var fs = require('fs');
//  var expat = require('node-expat');
  var fs = api.fs;
  var expat = api.node-expat;
  var inputFile = application.dir + '/files/okved/xml/SpOKVED-lite.xml';
  var outputFile = inputFile.replace(/xml/g,'json');

  console.log('\n           =========== start worker %s ===========\n','"upload"');
  console.log(' > upload.worker load data from: %s ', inputFile);

  var parser = new expat.Parser('UTF-8');   // вот тут Импресс выбрасывает ошибку
  var okveds = '';

  parser.on('startElement', function (name, attrs) {
    var okved = {};
    console.log(' > parse element <%s>', name);

    if (attrs.Id) {okved.id = attrs.Id};
    if (attrs.Kod) {okved.kod = attrs.Kod};
    if (attrs.GRUP) {okved.group = attrs.GRUP};
    if (attrs.Razdel) {okved.section = attrs.Razdel};
    if (attrs.Podrazdel) {okved.subSection = attrs.Podrazdel};
    if (attrs.OKVED) {okved.OKVED = attrs.OKVED};
    if (attrs.Name) {okved.name = attrs.Name};
    if (attrs.NameP) {okved.fullName = attrs.NameP};
    if (attrs.Prim) {okved.description = attrs.Prim.split('^').join('\n')};

    if ('OKVED' in okved) {
      console.log('   + done. Found okved %s',okved.id);
      okveds += JSON.stringify(okved)+'\n'; 
    }          
    else {console.log('   - in element <%s> okved not found',name)};
  });

  fs.readFile(inputFile, function(err, data) {
    if (err) {console.dir(err)};  
    parser.write(data);

//    fs.writeFile(outputFile, okveds, function(err) {
//      if(err) {console.log(err)} else {console.log(' +++ okved saved to %s',outputFile)};
//    });
  });

  console.log('           =========== worker %s end ===========','"upload"');
  callback();
};

If you pull the URL with a parameter, ?mode=uploadthen the worker is created, but it fails with an error:
=========== start worker "upload" ===========

 > upload.worker load data from:  <правильный путь до>/files/okved/xml/SpOKVED-lite.xml

2015-10-02T13:57:20.884Z        Worker(5100/C1L36)      [impress]       TypeError: application.domain.client.error is not a function
  EventEmitter.application.catchException (impress.application.js:69:33)
  Domain.fnWrapper (impress.client.js:789:25)
  Domain.run (domain.js:191:14)
  impress.client.js:803:33
  impress.application.js:147:9
  FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)
Impress shutting down

I have a feeling that something is wrong when including a module that was installed via npm.
I did npm install node-expat, after which I added 'node-expat' to the files config\sandbox.jsand applications\my-app\config\sandbox.js
what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-10-03
@dimon_durak

Now the connection of the modules has changed somewhat, for this case, add 'node-expat' to the api section in /applications/my-app/config/sandbox.js and it will be possible to access it as api.nodeExpat from any handler. Don't forget to update Impress version to today's version 0.1.372, it fixes the issue with replacing dashes in module names, i.e. by converting spinalToCamel, for example: node-expat to nodeExpat, because writing api['node-expat'] is somehow not beautiful, api.nodeExpat is better and I fixed it.
Now about the case with the upload, in your code for some reason it is not clear where the file upload occurs, and parsing starts immediately. If you want to do an upload, then you need to do it in the post.js handler, and then fork the process for parsing and for other long operations. In general, whether it makes sense to fork a process is a big question, because fork does not work quickly, and the new process eats memory, so if parsing is fast (takes tens of milliseconds), then there is no point in forking it, and if it is a few seconds of blocking the process , then yes - here you need to fork.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question