Узел — локальный сервер: [Ошибка: ENOENT: нет такого файла или каталога для XML-ответов от Postman

#javascript #node.js #express #postman

#javascript #node.js #экспресс #postman

Вопрос:

Я пытаюсь создать на локальном диске файлы журналов XML-запросов и ответов от Postman, следующие: https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657/4
https://documenter.getpostman.com/view/3407886/RWgp1fB5
https://blog.postman.com/write-to-your-local-file-system-using-a-postman-collection/https://community.postman.com/t/need-to-capture-response-body-of-each-iteration/3657

https://medium.com/apis-with-valentine/postman-how-to-write-files-to-disk-5ee398624a42

Проблема заключается в «[Ошибка: ENOENT: нет такого файла или каталога», когда сервер получает сообщения. Ошибка:

 Data is being stored at location: C:ResponseToFile-PostmanResponses
[Error: ENOENT: no such file or directory, open 'C:ResponseToFile-PostmanResponses{request.name || request.url}-${pm.info.iteraction}.log'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\ResponseToFile-Postman\Responses\{request.name || request.url}-${pm.info.iteraction}.log'
}
  

В Postman-Tests есть следующий скрипт:

 let dataToFile = {
    requestName: '{request.name || request.url}-${pm.info.iteraction}',
        fileExtension: 'log',
        responseData: pm.response.text()
};
pm.sendRequest({
        url: 'http://localhost:3000/write',
        method: 'POST',
        header: 'Content-Type:application/json',
        body: {
            mode: 'raw',
            raw: JSON.stringify(dataToFile).toString()
        }
}, function(err, res) {
        console.log(res);
});
  

Запуск узла scripts.js , где scripts.js есть

 const express = require('express'),
  app = express(),
  fs = require('fs'),
  shell = require('shelljs'),

   // Modify the folder path in which responses need to be stored
  folderPath = 'Responses',  //C:ResponseToFile-Postman
  defaultFileExtension = 'json', // Change the default file extension
  bodyParser = require('body-parser'),
  DEFAULT_MODE = 'writeFile',
  path = require('path');

// Create the folder path in case it doesn't exist
shell.mkdir('-p', folderPath);

 // Change the limits according to your response size
app.use(bodyParser.json({limit: '50mb', extended: true}));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); 

app.get('/', (req, res) => res.send('Hello, I write data to file. Send them requests!'));

app.post('/write', (req, res) => {
  let extension = req.body.fileExtension || defaultFileExtension,
    fsMode = req.body.mode || DEFAULT_MODE,
    uniqueIdentifier = req.body.uniqueIdentifier ? typeof req.body.uniqueIdentifier === 'boolean' ? Date.now() : req.body.uniqueIdentifier : false,
    filename = `${req.body.requestName}${uniqueIdentifier || ''}`,
    filePath = `${path.join(folderPath, filename)}.${extension}`,
    options = req.body.options || undefined;

  fs.appendFile(filePath, req.body.responseData, options, (err) => {
    if (err) {
      console.log(err);
      res.send('Error');
    }
    else {
      res.send('Success');
    }
  });
});

app.listen(3000, () => {
  console.log('ResponsesToFile App is listening now! Send them requests my way!');
  console.log(`Data is being stored at location: ${path.join(process.cwd(), folderPath)}`);
});
  

Полный журнал о запуске сервера и отправке ответа:

 PS C:ResponseToFile-Postman> node script2.js
(node:23172) Warning: Accessing non-existent property 'cat' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:23172) Warning: Accessing non-existent property 'cd' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'chmod' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'cp' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'dirs' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'pushd' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'popd' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'echo' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'tempdir' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'pwd' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'exec' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'ls' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'find' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'grep' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'head' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'ln' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'mkdir' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'rm' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'mv' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'sed' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'set' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'sort' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'tail' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'test' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'to' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'toEnd' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'touch' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'uniq' of module exports inside circular dependency
(node:23172) Warning: Accessing non-existent property 'which' of module exports inside circular dependency
ResponsesToFile App is listening now! Send them requests my way!
Data is being stored at location: C:ResponseToFile-PostmanResponses
[Error: ENOENT: no such file or directory, open 'C:ResponseToFile-PostmanResponses{request.name || request.url}-${pm.info.iteraction}.log'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\ResponseToFile-Postman\Responses\{request.name || request.url}-${pm.info.iteraction}.log'
}
  

Комментарии:

1. '{request.name || request.url}-${pm.info.iteraction}' очень похоже, что это должно быть на самом деле `${request.name || request.url}-${pm.info.iteraction}`

Ответ №1:

@Chris G разрешил это в комментарии.

'{request.name || request.url}-${pm.info.iteraction}' очень похоже, что это должно быть на самом деле ${request.name || request.url}-${pm.info.iteraction}