English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Use nodejs to monitor file changes and use sftp to upload to the server

I am using react recently+express to make a self-tool website (which is actually a treasure island bidding tool)

Then, because it often needs to be modified and tested on the server, we always have to go through webpack and then manually upload the files, which is very麻烦. So, I searched and wrote a script that can detect file changes and automatically upload them.

Firstly, we use npm to install two modules wrapped by others.

npm install ssh2-sftp-client
npm install gaze

The first module is used to upload files via sftp

The second module is used to listen to file changes. Of course, you can also use the built-in fs module of Node.js.

The usage of these two modules is as follows:ssh2-sftp-client gaze

After installation, the first step is to listen to file changes. Since my files have already been built with webpack, there will be no new files added later, so we only need to use 'changed' here. For other usage, please refer to the above link, which is similar

gaze(['Your file path/*.*','and you can also use an array to listen to multiple folders/app.js], function(err, watcher) {
 let watched = this.watched();
 //Listen to file changes
 this.on('changed', (filepath) => {
 //remotePath is the remote location of my file
 let remotePath = '"/root + filepath.substr(15;
 //The put function is for uploading files, which will be explained later 
 put(filepath, remotePath);
 console.log(filepath + was changed');
 });
}); 

Then we start writing our function to upload files

function put(localPath, remotePath){
 let sftp = new Client();
 sftp.connect({
 host: 'Your server address',
 port: 'port, if not changed, it is'22',
 username: 'connected username',
 password: 'password'
 }).then(() => {
 return sftp.put(localPath, romotePath);
 }).then(() =>{
 console.log("Upload completed");
 }).catch((err) => {
 console.log(err, 'catch error');
 });
}

Alright, don't forget to import the module at the beginning of our file.

let Client = require('ssh2-sftp-client');
let gaze = require('gaze');

Next, let's experiment. Come to our folder webpack

Sure enough, you can see that it has been modified and uploaded successfully. It takes time to upload, please be patient.

Come to our server, and indeed, the recent modification time has changed to now.

From now on, I no longer have to upload one by one. Every time I need to modify, just directly open a window to start this script, and then I can happily code.

The above is what the editor introduces to everyone about using nodejs to monitor file changes and upload them to the server using sftp, hoping it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Here we also thank everyone for their support of the Niyao tutorial website!

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You may also like