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!