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

Source: Internet
Author: User
This article mainly introduces how to use nodejs to monitor file changes and use sftp to upload to the server. It is very good and has reference value, for more information about how to use nodejs to monitor file changes and use sftp to upload files to the server, see this article.

Recently, react + express was used as a tool-type website (in fact, it is the attacker of the Island)

Then, because it often needs to be modified and put on the server for testing. Webpack is always required, and then manually upload the file. After simply searching, you can directly write a script that can detect file changes and automatically upload the file.

First, we use npm to install two modules encapsulated by others.


npm install ssh2-sftp-clientnpm install gaze


The first module is used to upload files through sftp,

The role of the second module is to listen for file changes. Of course, you can also use the node built-in fs module.

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

After the installation is complete, the first step is to listen for changes to the file. Since my file has been built using webpack, only file changes will be followed, and no files will be added, so here you only need to use changed. For other usage instructions, refer to the above link.


Gaze (['Your file path /*. * ',' can also use arrays to listen to Multiple folders/apps. js'], function (err, watcher) {let watched = this. watched (); // listen for file changes this. on ('changed ', (filepath) => {// romotePath is the remote location of my file. let romotePath ='/root' + filepath. substr (15); // put is the function for uploading files. The following describes put (filepath, romotePath); console. log (filepath + 'was changed ');});});


Then we start to write our function for uploading files.


Function put (localPath, romotePath) {let sftp = new Client (); sftp. connect ({host: 'Your server address', port: 'port, if it hasn't been changed, it's 22', username: 'Connection username ', password: 'Password '}). then () => {return sftp. put (localPath, romotePath );}). then () => {console. log ("uploaded ");}). catch (err) => {console. log (err, 'catch error ');});}


Well, don't forget to introduce the module at the beginning of our file.


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


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

The last modification time on our server has become now.

Since then, I have no need to upload files one by one. Each time you want to modify it directly. Open a window and open the script to enable the encoding.

The above section describes how to use nodejs to monitor file changes and use sftp to upload files to the server. I hope it will be helpful to you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. I would like to thank you for your support for PHP chinnet!

For more information about how to use nodejs to monitor file changes and use sftp to upload files to the server, see PHP!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.