Some small but useful Node.js packages

Some small but useful Node.js packages

Front-end Pioneer

Some small but useful Node.js packages

yargs

yargs is a package for processing command line parameters, which can help you deal with the command line flags set by yourself and any type of input data, including boolean values, floating point numbers, and strings. This package is very simple and clear, without writing a lot of boilerplate code in the project.

Yargs can help you deal with the "usage help" output, and can easily tell users which options need to be entered when using your program, including which ones are required.


var argv = require('yargs')
    .usage('Usage: $0 -x [num] -y [num]')
    .demand(['x','y'])
    .argv;

console.log('Pow(x, y):', Math.pow(argv.x, argv.y));

Save the above code as index.js, and then execute node index.js -x 3 in the command line, you will see the following message:


Usage: index.js -x [num] -y [num]

Options:
  -x                                   [required]
  -y                                   [required]

Missing required argument: y

yargs can tell us what parameters are missing from the command line, and we only need to simply call the .usage() and .demand() methods.

toobusy

This is a very practical package. It polls the Node event loop and tracks the time required to complete the request. If it finds that the delay is too long, toobusy will notify you, and then you can return the HTTP 503 "Service Unavailable" status code to the client.

This processing is very important because the busier the server, the longer the waiting time for the request. This quickly becomes a very complex issue, which will become more and more serious as time passes. If you leave it alone, the service will collapse. If we can stop the processing of some requests in time and return HTTP 503, then at least some requests can be processed.

You can easily install toobusy with the npm command:


npm install toobusy

Then integrate it with something like Express:


var toobusy = require('toobusy'),
    express = require('express');

var app = express();

// 如果服务器压力过大将会阻止请求
app.use(function(req, res, next) {
 if (toobusy()) {
  res.send(503, "Too many users!");
 } else {
  next();
 } 
});

var server = app.listen(3000);

process.on('SIGINT', function() {
 server.close();
 toobusy.shutdown(); // 正常退出
 process.exit();
});

No need to write much code or too much configuration to inherit into our own project.

chalk

It is difficult to develop a useful user interface on the command line, because only the command line window is used to interact with the user. So how do you prompt some important information? It is a good way to add formatting to the output text. Express is a typical example. From its output, you can easily find important information quickly.

The following is a list of styles supported by chalk:

</> modifier

  • bold
  • underline
  • dim
  • reset
  • hidden
  • inverse
  • italic (not supported by all environments)
  • strikethrough (not supported in any environment)

    </> color

  • red
  • black
  • green
  • white
  • yellow
  • blue (the brighter version is used on Windows, because ordinary blue is difficult to recognize)
  • cyan
  • gray
  • magenta

    </> background color

  • bgBlue
  • bgBlack
  • bgRed
  • bgGreen
  • bgCyan
  • bgYellow
  • bgWhite

  • Although bgMagenta officially only supports these colors, any terminal that conforms to the xterm standard can use the full 8-bit color code.

The text can be easily formatted by passing the string to the function used for coloring or formatting. If you need to let users notice a serious error message, you can use the following format:


var chalk = require('chalk');

var str = chalk.red.bold('ERROR: ') + chalk.bold('Everything just blew up...');
console.log(str);

node-inspector

It is difficult to find useful debuggers, especially those with a useful GUI. Node-inspector provides you with a web GUI to help you debug your code. It has all the functions of a standard debugger, such as breakpoints, single-step execution, exit codes, variable checking, etc. There are also some less commonly used functions, but these functions are very useful, such as CPU and heap analysis, network client requests The function of checking and editing running code in real time.
Some small but useful Node.js packages

node-inspector
However, node-inspector is only compatible with Chrome and Opera, because it uses Blink Developer Tools and is compatible with Node.

I have always relied heavily on console output for debugging, which would take a lot of time. Using GUI can greatly save debugging time.

terminal-kit

If your Node program needs to support other operations besides simple text input and output under the command line, then you should need terminal-kit. terminal-kit simplifies many things that interact with users, allowing you to focus on developing important content in the program. The main functions of terminal-kit are:

  • Text style (much like chalk)
  • Edit screen
  • progress bar

  • There are many examples of user input suitable for terminal toolkits. For example, if you need to download some content from the Internet, then you need to display a progress bar to the user. The following code is used to display the virtual progress bar:

var terminal = require( 'terminal-kit' ).terminal;

var progressBar;
var progress = 0;

function updateProgress() {
    // 产生一个随机的进度值
    progress += Math.random() / 10;
    progressBar.update(progress);

    // 检查是否完成
    if (progress >= 1) {
        setTimeout(function() {
         terminal('\n');
         process.exit();
        }, 250);
    }
    else {
        setTimeout(updateProgress, 100 + Math.random() * 500);
    }
}

progressBar = terminal.progressBar({
    width: 80,
    title: 'Downloading file:',
    eta: true,
    percent: true
});

updateProgress();

The above code will produce the following effect:
Some small but useful Node.js packages

terminal-kit progress bar

validator

The validator can help you perform a series of common string verification (for example: email address, phone number, IP address, etc.). Whenever you get input from the user, such a package is essential. Users will make mistakes and enter some very strange things in the text box, so a package to verify the input is needed to avoid data corruption or server crashes.

Here are some commonly used validators:

  • isEmail(str [, options])
  • isIP (str [, version])
  • isMobilePhone (str, local)
  • isURL(str [, options])
    validator also provides a detector that can normalize, delete or escape the input string. For example, clean up the content submitted by users to prevent them from entering malicious HTML or JavaScript code.

The following are commonly used detectors:

  • blacklist(input, chars)
  • escape(input)
  • normalizeEmail(email [, options])
  • The whitelist(input, chars)
    normalizeEmail() method can ensure that email addresses are all lowercase letters, and even delete characters that need to be ignored. Suppose you have an email [email protected], normalizeEmail() will normalize it to [email protected].

formidable

formidable can help you handle every step of file upload, including multi-part parser, writing files to disk, and error handling. This is one of my favorite bags, if you don't want to reinvent the wheel, you can give it a try.

The following is an example of using formidable on a normal HTTP server. The code is modified from the example given in the package itself:


var http = require('http');
var util = require('util');
var formidable = require('formidable');
var path = require('path');

var PORT = 8080;

var root = path.join(__dirname, '../');
exports.dir = {
 root    : root,
 lib     : root + '/lib',
 fixture : root + '/test/fixture',
 tmp     : root + '/test/tmp',
};

var server = http.createServer(function(req, res) {
  if (req.url == '/') {
    res.writeHead(200, {'content-type': 'text/html'});
    res.end(
      '<form action="/post" method="post">' +
      '<input type="text" name="title"><br>' +
      '<input type="text" name="data[foo][]"><br>' +
      '<input type="submit" value="Submit">' +
      '</form>'
    );
  } else if (req.url == '/post') {
    var form = new formidable.IncomingForm(),
        fields = [];

    form
      .on('error', function(err) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.end('error:\n\n' + util.inspect(err));
      })
      .on('field', function(field, value) {
        console.log(field, value);
        fields.push([field, value]);
      })
      .on('end', function() {
        console.log('-> post done');
        res.writeHead(200, {'content-type': 'text/plain'});
        res.end('received fields:\n\n ' + util.inspect(fields));
      });
    form.parse(req);
  } else {
    res.writeHead(404, {'content-type': 'text/plain'});
    res.end('404');
  }
});

server.listen(PORT);

console.log('listening on http://localhost:' + PORT + '/');

shelljs

shelljs is a package that allows you to use common Unix commands on any system, whether it is Windows, Linux or Mac. This way you no longer need to write separate bash and batch scripts for the project. shelljs provides a Unix-like environment. If you need to write a script to run tests, submit code or start on the server, you only need to write it once.

You can perform similar operations with commands:


require('shelljs/global');

ls('*.js').forEach(function(file) {
 sed('-i', 'BUILD_VERSION', 'v2.0.3', file);
 sed('-i', /.*REMOVE_THIS_LINE.*\n/, '', file);
 sed('-i', /.*REPLACE_THIS_LINE.*\n/, cat('macro.js'), file);
});

Execute common commands:


require('shelljs/global');

mkdir('-p', 'release/data');
cp('-R', 'data/*', 'release/data');

Check the available binaries:


require('shelljs/global');

if (!which('git')) {
 echo('This script requires git!');
 exit(1);
}

You can even run commands like in a bash script:


if (exec('git commit -am "Release commit"').code !== 0) {
  echo('Error: Git commit failed!');
  exit(1);
}

Do you know any useful packages? Please leave a message in the comments.

Guess you like

Origin blog.51cto.com/15077562/2609656