I need to create a node.js app that connects to this ftp server and downloads files from this directory:
ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb
I’ve tried following the ftp
npm package docs but I feel like I am doing something horribly wrong:
JavaScript
x
24
24
1
import Client from "ftp";
2
3
/**
4
* https://github.com/mscdex/node-ftp
5
*/
6
7
const c = new Client();
8
9
c.on("ready", function () {
10
c.get(
11
"ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb",
12
(error, stream) => {
13
if (error) throw error;
14
console.log(`stream`, stream);
15
stream.once("close", function () {
16
c.end();
17
});
18
}
19
);
20
});
21
22
// connect to localhost:21 as anonymous
23
c.connect();
24
When I run npm run dev
with nodemon
I get:
JavaScript
1
4
1
Error: connect ECONNREFUSED 127.0.0.1:21
2
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
3
[nodemon] app crashed - waiting for file changes before starting
4
Can someone please help? I’m completely stumped.
Is it possible if someone could show me a small example of how I can connect to this remote ftp server?
Advertisement
Answer
There are a few points :
- You’re connecting to the local ftp with
c.connect();
. You need to connect towww.ngs.noaa.gov
to download files from there. - This path
cors/rinex/2021/143/nynb
is a directory on the remote host.c.get
doesn’t work, you need to list all files in the directory then download them 1 by 1.
The code below connect to the remote server and list all files in the directory
JavaScript
1
15
15
1
const Client = require('ftp');
2
const fs = require("fs");
3
const c = new Client();
4
5
c.on('ready', function () {
6
c.list( "/cors/rinex/2021/143/nynb", function (err, list) {
7
if (err) throw err;
8
console.dir(list);
9
});
10
});
11
12
c.connect({
13
host: "www.ngs.noaa.gov",
14
});
15