I've got some script in Node.js which is working when I run it on my computer but not when I'm on a server. I've got 2 servers: I run the webApplication on the first (Server 1) and the second (Server 2) contains files.
var fs = require('fs');
var FTPClient = require('ftp');
var c = new FTPClient();
c.connect({
host: "192.168.200.2",
user: "*****",
password: "******"
});
c.on('ready', function() {
console.log("Connected to datastore !");
var dateFile = './Dictionnary/lastUpdate.json';
var csvFile = '//192.168.200.2/Alfa/file4457.csv'
var lastUpdate;
fs.stat(csvFile, (err, stat) => {
if (err) {
console.error(err);
};
var date = { mtime: stat.mtime }
var lastDate = new Date(jsonfile.readFileSync(dateFile).mtime);
var newDate = new Date(stat.mtime);
if (newDate <= lastDate) {
console.log('Dictionnary up-to-date');
} else {
console.log('Update Needed');
var DeleteQuery = mySqlClient.query('DELETE FROM dictionnary');
csv = fs.readFile(csvFile, 'utf-8', (err, data) => {
if (err) {
throw err
};
dictionnary = parseCSV(data);
for (i = 0; i < dictionnary.length - 1; i++) {
var insertQuery = ("INSERT INTO Dictionnary VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
var query = mySqlClient.query(insertQuery, [dictionnary[i][0], dictionnary[i][1], dictionnary[i][2], dictionnary[i][3], dictionnary[i][4], dictionnary[i][5], dictionnary[i][6], dictionnary[i][7], dictionnary[i][8], dictionnary[i][9], dictionnary[i][10], , dictionnary[i][11], , dictionnary[i][12]]);
};
}
}
}
When I run the script on my computer I can find the file on the (Server 2) but when I run it on the (server 1) it doesn't work anymore and I've this error:
Error: ENOENT: no such file or directory, open '//192.168.200.2/Alfa/file4457.csv'
I thought it was a problem with the pass so I tried with //192.168.200.2/Alfa/file4457.csv
, ./Alfa/file4457.csv
, Alfa/file4457.csv
But that's not working.
I checked the path and he's good.
For the fs.stat()
that's not a problem I can still replace it by c.lastMod()
but I don't find what I can use to replace the fs.readFile()
Thank you for reading.
fs
will look the path you give to it //192.168.200.2/Alfa/file4457.csv
.
From the server you're executing the script, this path is not found. I don't know your network and servers architecture so I can't really explain why it works on your computer and not on the server. But I guess your computer and the FTP server are on the same local network (correctme if I'm wrong).
After reading your code, you're opening a FTP connection on a remote FTP server :
c.connect({
host: "192.168.200.2",
user: "*****",
password: "******"
});
c.on('ready', function() {
...
});
But int the .on('ready', function() { ... })
callback you don't do anything with this new openned connection (look at your code, you never use c
again).
By reading the ftp
node module documentation, you can find that there is the get
function, able to retrieve a file from your FTP connection.
get(< string >path, [< boolean >useCompression, ]< function >callback) - (void) - Retrieves a file at path from the server. useCompression defaults to false. callback has 2 parameters: < Error >err, < ReadableStream >fileStream.
So, you have to use the FTP connection with something like that :
c.connect({
host: "192.168.200.2",
user: "*****",
password: "******"
});
c.on('ready', function() {
c.get('/Alfa/file4457.csv', function(err, stream) {
var content = '';
stream.on('data', function(chunk) {
content += chunk.toString();
});
stream.on('end', function() {
// content variable now contains all file content.
});
})
});