current position:Home>Learn about using node (I)

Learn about using node (I)

2021-08-27 08:53:22 Front end worker

This is my participation 8 The fourth of the yuegengwen challenge 23 God , Check out the activity details :8 Yuegengwen challenge

brief introduction

Node.js It's based on Chrome V8 Engine JavaScript Runtime , You can set up a server to run JavaScript Applications , You can also write some scripts yourself .

install

We can log in node.js Official website installation LTS edition ( Stable version ), After the installation, we check whether the installation is successful :

7BA10014-2827-4b5e-8B55-3692415F3E8E.png

Console execution npm -v and node -v The version number indicates that the installation is successful

Use node Make one HTTP The server

  1. introduce required modular
var http = require("http");
 Copy code 
  1. createServer() Method to create a server
var http = require('http');
http.createServer(function (request, response) {

    //  send out  HTTP  Head  
    // HTTP  The status value : 200 : OK
    //  Content type : text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});

    //  Send response data  "Hello World"
    response.end('Hello World\n');
}).listen(8888);

//  The terminal prints the following information 
console.log('Server running at http://127.0.0.1:8888/')

 Copy code 

So our web The server is created , Can be accessed by 127.0.0.1:8888 To visit your own web.

Use node Develop your own tool scripts

Let's implement a requirement : Need to put css In the file px The unit is converted into rem Company

  1. introduce fs,path modular
var fs = require("fs");
var path = require('path');
 Copy code 

fs and path Two modules are convenient for us to operate folders and files

  1. Traverse the target folder

Prepare a dish containing css File folder , Traverse the target folder , Get its file list :

var fs = require("fs");
var path = require('path'); // Resolve the folders that need to be traversed 
var filePath = path.resolve('./css');
fileDisplay(filePath);

function isDir(path){  //  Is it a directory 
    return fs.statSync(path).isDirectory();  
}  

function fileDisplay(filePath) {
   // Read the file according to the file path , Return to file list 
   fs.readdir(filePath, function(err, files) {
       if(err) {
           console.warn(err, 111)
       } else {
           // Traverse the read file list 
           files.forEach(function(filename) {
               var s = path.join(filePath, filename)
               if (isDir(s)) {  //  Catalog 
                fileDisplay(s)
               } else {   //  file 
                   handleFile(s)
               }
           });
       }
   });
}
 Copy code 
  1. Prepare regular expression rules corresponding to requirements , Process the obtained file list , Overwrite the original file after processing :
function handleFile(path) {
   fs.readFile(path, 'utf8', function(err, data) {
       if(err) {
           console.warn(err)
       } else {
           //css Partial capture 
           if(path.indexOf(".css") != -1) {
               replaceCss(data, path)
           }
       }
   });
}

function replaceCss(fileContent, fileName) {
   var str = fileContent;
   str = str.replace(/\d*.?\d+px/g, function(a) {
      console.log('rem', a)
      return parseFloat(a) + 'rem';
   });
   fs.writeFileSync(fileName, str);
}
 Copy code 

With such a simple scripting tool, we have completed , You can use your imagination to write some scripts to deal with some daily development work and reduce cumbersome repeated operations .

copyright notice
author[Front end worker],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210827085319320I.html

Random recommended