简体   繁体   中英

Download the file from a remote SFTP server in Node.js + Vue.js?

In my Vue.js application I need to download .csv file from a remote SFTP server.

I am tring to use ssh2-sftp-client Node.js library to download the file.

After installation I have next error:

This dependency was not found:

* dns in ./node_modules/ssh2/lib/client.js

To install it, you can run: npm install --save dns

I installed the missing dependencies with the following command:

npm install --save dns

After that step I see other error:

 warning  in ./node_modules/defaultable/defaultable.js

49:13-31 Critical dependency: the request of a dependency is an expression

First question is how to set up the library correctly?


EDIT

Code inside Vue.js component:

getFile (fileName) {
    axios.post('http://localhost:3010/csv', {file_name: fileName}, {headers: {'Authorization': this.token}}).then(response => {
        console.log(response)
        this.showAlert('You download file successfully.', 'is-success', 'is-top')
    }).catch((error) => {
        console.log(error)
        this.showAlert(error, 'is-danger', 'is-bottom')
    })
}

app.js:

const express = require('express');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');
const path = require('path');
const bodyParser = require('body-parser');

const csvRouter = require('./server/routes/csv')

const app = express();

app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'dist')));
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());

app.use('/csv', csvRouter);

module.exports = app;

routers/csv.js:

const express = require('express')
const router = express.Router()

const csvControllers = require('../controllers/csv')

router.get('/', csvControllers.getFile)

module.exports = router

controllers/csv.js:

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)  // In console I don't notice nothing.
  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('Message')  // In console I don't notice nothing.
    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)
    }
  })
}

ERROR:

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)

It seems like url address don't correct cause error is 404 in browser. I check url address several times but did't notice something wrong. What I miss?

Vue.js runs in the browser. You can't use modules that depend on features provided by Node.js which are not available in browsers.

There is no way to use SSH directly from a browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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