npm install tough-cookie-ftp-store --save
const { CookieJar, Cookie } = require('tough-cookie');
const { FtpCookieStore } = require('tough-cookie-ftp-store');
const ftpConfig = {
host: 'example.com',
port: 21,
user: 'my-username',
password: 'my-secret-password',
secure: false
};
const filePath = '/cookies/my-cookies.json';
const main = async () => {
// create client
const store = new FtpCookieStore(filePath, {
timeout: 30000, // ftp connection timeout, in ms (30 seconds)
debug: false
});
// connect to ftp server
await store.connect(ftpConfig);
// create a cookie jar
const cookieJar = new CookieJar(store);
// set an example cookie
const cookie = Cookie.parse('foo=bar; Domain=example.com; Path=/');
// put cookie to cookie jar
await cookieJar.setCookie(cookie, 'http://example.com', (error, cookie) => {
// handle errors
if (error) {
console.log(error);
}
// show cookie
console.log(cookie);
// kill ftp connection
store.disconnect();
});
}