转自原文 nodejs 中使用 ftp
1. npm install ftp
项目 https://github.com/mscdex/node-ftp
2. 转自 http://www.open-open.com/lib/view/open1408006289661.html
Node.js的FTP客户端模块,提供了一个用于与FTP服务器进行通信的异步接口。
-
- 获取当前的(远程)工作目录的目录列表:
123456789101112
var
Client = require(
'ftp'
);
var
c =
new
Client();
c.on(
'ready'
,
function
() {
c.list(
function
(err, list) {
if
(err)
throw
err;
console.dir(list);
c.end();
});
});
// connect to localhost:21 as anonymous
c.connect();
- 获取当前的(远程)工作目录的目录列表:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var
Client = require(
'ftp'
);
var
fs = require(
'fs'
);
var
c =
new
Client();
c.on(
'ready'
,
function
() {
c.get(
'foo.txt'
,
function
(err, stream) {
if
(err)
throw
err;
stream.once(
'close'
,
function
() { c.end(); });
stream.pipe(fs.createWriteStream(
'foo.local-copy.txt'
));
});
});
// connect to localhost:21 as anonymous
c.connect();
|
-
- 上传本地文件“foo.txt'到服务器:
1
2
3
4
5
6
7
8
9
10
11
12
|
var
Client = require(
'ftp'
);
var
fs = require(
'fs'
);
var
c =
new
Client();
c.on(
'ready'
,
function
() {
c.put(
'foo.txt'
,
'foo.remote-copy.txt'
,
function
(err) {
if
(err)
throw
err;
c.end();
});
});
// connect to localhost:21 as anonymous
c.connect();
|
http://www.open-open.com/lib/view/home/1408006289661