N
N
nurzhannogerbek2019-01-25 15:57:26
JavaScript
nurzhannogerbek, 2019-01-25 15:57:26

How to download file from sftp server to Node.js + Vue.js?

Hello comrades! Please help me figure it out.
User requests from a Vue.js application are processed in a Node.js HTTP web server. In general, the task is to download a file from a remote sftp server via a web interface. Here's what I did. When a certain button is clicked, the getFile
function is launched into which I pass the file name. In the getFile function, I make a post request to a specific url address. This url handles the controller in Node.js. On the Node.js side, the file is connected and downloaded using the ssh2-sftp-client library . At the moment, the error is on the Vue.js side. The request does not reach Node.js, I think. Where did you miss it. Tell me please.

Error: Request failed with status code 404
    at FtD3.t.exports (createError.js:16)
    at t.exports (settle.js:18)
    at XMLHttpRequest.f.(:3010/anonymous function) (http://localhost:3010/static/js/vendor.1dc24385e2ad03071ff8.js:1312:88758)

The code from the Vue.js component is the following:
getFile (fileName) {
    axios.post('http://localhost:3010/csv', {file_name: fileName}, {headers: {'Authorization': this.token}}).then(response => {
        console.log(response)
        this.showAlert('Файл скачен.', 'is-success', 'is-top')
    }).catch((error) => {
        console.log(error)
        this.showAlert(error, 'is-danger', 'is-bottom')
    })
}

controller:
const request = require('request')
const queryString = require('query-string')
let Client = require('ssh2-sftp-client')
let sftp = new Client()

const config = require('../config')

exports.getFile = (req, res) => {
  console.log(req)  // В консоль ничего не выводится
  let data = {
    file_name: req.query.file_name
  }
  let options = {
    method: 'port',
    json: true,
    header: {'Authorization': req.header.token},
    url: `http://localhost:3010/csv?` + queryString.stringify(data)
  }
  request(options, (error, response) => {
    console.log('Выполняется блок')  // <- В консоль ничего не выводится.
    if (response) {
      sftp.connect(config.sftpServer).then(() => {
        return sftp.get('/reports/' + data.file_name)
      }).then((chunk) => {
        console.log(chunk)
      }).catch((err) => {
        console.log(err)
      })
    } else {
      response.status(500).send(error)
    }
  })
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question