51
头图

TNTWeb-the full name of Tencent News Central Taiwan Front-end Team, the small partners in the group have practiced and accumulated in the front-end web, NodeJS development, UI design, mobile APP and other big front-end fields.

At present, the team mainly supports the front-end development of Tencent News's various businesses. In addition to business development, it has also accumulated some front-end infrastructure to empower business efficiency and product innovation.

The team advocates open source and co-construction, and has a variety of technical experts. The team’s Github address: https://github.com/tnfe

The author of this article Yunfeng Project address: https://github.com/tnfe/mdebug

image

A long time ago, software engineers accelerated the development process by reducing repetitive code in the program. The Nodejs ecosystem reuses code in the form of npm packages, and there are currently more than 1 million open source packages available. From small Internet projects to well-known technology startups, npm packages are used. Some of the popular packages are downloaded more than 10 million a week and are the basis of many applications. Today, a lot of code in modern web applications comes from npm modules. We have selected 30 commonly used nodejs installation packages to save you from reinventing the wheel.

1. Practical functions

1. qs

A simple and easy-to-use string parsing and formatting library
const qs = require('qs');
constv assert = require('assert');

const obj = qs.parse('a=c');
assert.deepEqual(obj, { a: 'c' });

const str = qs.stringify(obj);
assert.equal(str, 'a=c');

2.rxjs

RxJS is a set of modular libraries for composing asynchronous and event-based programs using observable collections and combinations in JavaScript.
const { range } = require('rxjs');
const { map, filter } = require('rxjs/operators');
range(1, 200).pipe(
  filter(x => x % 2 === 1),
  map(x => x + x)
).subscribe(x => console.log(x));

3. mitt

Mini 200b feature event launcher/publish and subscribe.
import mitt from 'mitt'
const emitter = mitt()
emitter.on('foo', e => console.log('foo', e) )
emitter.on('*', (type, e) => console.log(type, e) )
emitter.emit('foo', { a: 'b' })
emitter.all.clear()

function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

4.Underscore.js

Underscore.js is a utility library for JavaScript, which provides support for the usual suspects (each, map, reduce, filter, etc.) without extending any core JavaScript objects.
const _ = require(“underscore);
const list = [[5, 1, 7], [3, 2, 1]];
_.invoke(list, 'sort');
// => [[1, 5, 7], [1, 2, 3]]

5.day.js

Day.js is a minimalist JavaScript library that parses, verifies, manipulates and displays dates and times for modern browsers, and has an API that is largely compatible with Moment.
const dayjs = require(“dayjs”);
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');

6.Ramda

Ramda is a practical functional library with side-effect-free functions that can be combined with currying.
import * as R from 'ramda';
const double = x => x * 2;
R.map(double, [1, 2, 3]); 
// => [2, 4, 6]
R.map(double, {x: 1, y: 2, z: 3}); 
// => {x: 2, y: 4, z: 6}

7.validator

Validator is a string validator and cleaner library.
var validator = require('validator');
validator.isEmail('foo@bar.com'); //=> true

8.yup

yup is a pattern builder for complex, interdependent verification and transformation.
import * as yup from 'yup';
let schema = yup.object().shape({
  name: yup.string().required(),
  age: yup.number().required().positive().integer(),
  email: yup.string().email(),
  website: yup.string().url(),
  createdOn: yup.date().default(function () {
    return new Date();
  }),
});
// check validity
schema
  .isValid({
    name: 'jimmy',
    age: 24,
  })
  .then(valid => 
    console.log(valid) // => true
  );
// you can try and type cast objects to the defined schema
schema.cast({
  name: 'jimmy',
  age: '24',
  createdOn: '2014-09-23T19:25:25Z',
});
// => { name: 'jimmy', age: 24, createdOn: Date }

9.Lodash

Lodash is a utility library that makes JavaScript easier by eliminating the hassle of dealing with arrays, numbers, objects, strings, etc.
const _ = require("lodash");
const nums = _.range(1, 9);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]
const chunks = _.chunk(nums, 3);
// => [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const right = _.takeRight(nums, 2);
// => [7, 8, 9]

10.date-fns

Date-fns provides the most comprehensive, simple and consistent toolset for manipulating JavaScript dates in the browser and Node.js.
import { format, formatDistance, formatRelative, subDays } from 'date-fns'
format(new Date(), '[Today is a] dddd')
//=> "Today is a Wednesday"
formatDistance(subDays(new Date(), 3), new Date())
//=> "3 days ago"
formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."

11.jsonwebtoken

Jsonwebtoken is a library for signing, verifying, and decoding JSON Web tokens.
const jwt = require('jsonwebtoken');
const token = jwt.sign({ foo: 'bar' }, 'shhhhh');

12.uuid

UUID is a library for creating RFC4122 universally unique identifiers.
const { v4: uuidv4 } = require('uuid');
uuidv4(); // => '1a68a438-b077-468b-b1e8-dcdd976a0f5b'

Two, operating the file system

1.rimraf

Rimraf provides nodes with commands equivalent to the UNIX rm -rf command.
const rimraf = require(“rimraf”);
rimraf('./build', error => {
  if (error) console.error(error);
});

2.fs-extra

FS-extra adds file system methods that are not included in the native fs module, and adds promise support for the fs method.
const fs = require(‘fs-extra’);
async function copyFiles () {
  try {
    await fs.copy('/tmp/myfile', '/tmp/mynewfile');
    console.log('success!');
  } catch (err) {
    console.error(err);
  }
}
copyFiles();

3.mkdirp

Just like mkdir -p, mkdirp recursively creates the directory and all necessary subdirectories.
const mkdirp = require('mkdirp')
// return value is a Promise resolving to the first directory created
mkdirp('/tmp/foo/bar/baz').then(made =>
  console.log(`made directories, starting with ${made}`));

4.glob

Glob is a library that uses multiple patterns to match files.
const glob = require("glob");
// options is optional
glob("**/*.js", options, function (er, files) {
  // files is an array of filenames.
  // If the `nonull` option is set, and nothing
  // was found, then files is ["**/*.js"]
  // er is an error object or null.
});

5.shelljs

ShellJS is a portable (Windows/Linux/OS X) implementation of Unix shell commands based on Node.js API.
const shell = require('shelljs');
if (!shell.which('git')) {
  shell.echo('Sorry, this script requires git');
  shell.exit(1);
}
// Copy files to release dir
shell.rm('-rf', 'out/Release');
shell.cp('-R', 'stuff/', 'out/Release');
// Replace macros in each .js file
shell.cd('lib');
shell.ls('*.js').forEach(function (file) {
  shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file);
  shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file);
  shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file);
});
shell.cd('..');
// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
  shell.echo('Error: Git commit failed');
  shell.exit(1);
}

6.js-yaml

Js-yaml is an implementation of YAML, which is a popular humanized data serialization language.
const yaml = require('js-yaml');
const fs   = require('fs');
// Get document, or throw exception on error
try {
  const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8'));
  console.log(doc);
} catch (e) {
  console.log(e);
}

Three, web framework

1. koa

Koa is a new web framework designed by the team behind Express, aiming to become a smaller, more expressive and robust foundation for web applications and APIs.
const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Hello World';
});
app.listen(3000);

2. express

Express is the most popular, fastest, and simplest node.js backend web framework.
const express = require('express');
const app = express();
app.get('/', function (req, res) {
  res.send('Hello World');
});
app.listen(3000);

3. Fastify

Fastify is one of the fastest extensible web frameworks, focusing on providing the best developer experience with minimal overhead.

const fastify = require('fastify')({
  logger: true
});
fastify.get('/', async (request, reply) => {
  reply.type('application/json').code(200);
  return { hello: 'world' };
});
fastify.listen(3000, (err, address) => {
  if (err) throw err;
  fastify.log.info(`App listening on ${address}`);
});

4. socket.io

Socket.IO uses long polling or WebSockets to enable real-time two-way event-based communication, and has disconnect detection and automatic reconnection support.
const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);

image

Four, auxiliary development

1. jest

Jest is complete and ready to set up a JavaScript testing solution
test('adds 1 + 2 to equal 3', () => {
  expect(1 + 2).toBe(3);
});

2. typescript

TypeScript is extensible JavaScript. It is a language that adds optional types and compiles to normal readable JavaScript.
interface User {
  name: string;
  id: number;
}
const user: User = {
  name: "Hayes",
  id: 0,
};

3.winston

Winston is a simple and universal log library that supports multiple transmissions.
const winston = require('winston');
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  defaultMeta: { service: 'user-service' },
  transports: [
    // Write all logs with level `error` and below to `error.log`
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    // Write all logs with level `info` and below to `combined.log`
    new winston.transports.File({ filename: 'combined.log' }),
  ],
});
logger.log({
  level: 'error',
  message: 'Hello distributed log files!'
});
logger.info('Hello again distributed logs');

4.debug

Debug is a tiny JavaScript debugging utility that imitates the core debugging technology of Node.js.
const debug = require('debug')('http')
  , http = require('http')
  , name = 'My App';
debug('booting %o', name);
http.createServer(function(req, res){
  debug(req.method + ' ' + req.url);
  res.end('hello\n');
}).listen(3000, function(){
  debug('listening');
});

5. eslint

ESLint is a tool for finding and fixing problems in JavaScript and TypeScript code.
{
    "rules": {
        "semi": ["error", "always"],
        "quotes": ["error", "double"]
    }
}

6. nodemon

Nodemon is a tool that helps develop node.js-based applications by automatically restarting the node application when it detects a file change in the directory.
nodemon ./server.js 

7. dotenv

Dotenv is a zero-dependency module that can load environment variables in the .env file into process.env
.env file:

DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
require('dotenv').config();
const db = require('db');
db.connect({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS
});

8. cross-env

Cross-env enables scripts to set and use environment variables across platforms.
{
  "scripts": {
    "start-prod": "cross-env NODE_ENV=production node ./app.js"
  }
}

image


TNTWEB
3.8k 声望8.5k 粉丝

腾讯新闻前端团队