1

I am trying to create a simple API in node that connects to a remote via sftp and lists directory contents. It only works on the first request and any following requests produces the following error.

Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

I'm not sure how to fix this.

My code

import { Router } from "express";
import Client from "ssh2-sftp-client";
const router = Router();
let sftp = new Client();

router.get("/", (req, res) => {
  sftp
    .connect({
      host: "localhost",
      port: "22",
      username: "test",
      password: "*******"
    })
    .then(() => {
      return sftp.list("/");
    })
    .then(data => {
      sftp.end();
      res.send(data);
    })
    .catch(err => {
      console.log(err, "catch error");
    });
});

export default router;
jerox
  • 31
  • 1
  • 3

0 Answers0