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:
import Client from "ftp"; /** * https://github.com/mscdex/node-ftp */ const c = new Client(); c.on("ready", function () { c.get( "ftp://www.ngs.noaa.gov/cors/rinex/2021/143/nynb", (error, stream) => { if (error) throw error; console.log(`stream`, stream); stream.once("close", function () { c.end(); }); } ); }); // connect to localhost:21 as anonymous c.connect();
When I run npm run dev
with nodemon
I get:
Error: connect ECONNREFUSED 127.0.0.1:21 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) [nodemon] app crashed - waiting for file changes before starting...
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
const Client = require('ftp'); const fs = require("fs"); const c = new Client(); c.on('ready', function () { c.list( "/cors/rinex/2021/143/nynb", function (err, list) { if (err) throw err; console.dir(list); }); }); c.connect({ host: "www.ngs.noaa.gov", });