Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ How to become a better Node.js developers in 2016     - Ceph Source Analysis: Network Module (Server)

- Linux use glibc2 library and crypt () function to generate the user password (Programming)

- MySQL to recover the data through binlog (Database)

- Get the Linux device PCI ID method (Linux)

- CentOS 7 update source - the use of domestic sources 163 yum (Linux)

- Ubuntu 14.04 configure JDK1.8.0_25, switchable version (Linux)

- MySQL display operation control tips (Database)

- Linux Platform Oracle 11g Single Instance Installation Deployment Configuration Quick Reference (Database)

- To configure Samba to share files with Windows under CentOS (Linux)

- Android child thread really do not update UI (Programming)

- Linux command execution judge sentences -;, &&, || (Linux)

- Oracle procedure or function Empty Table (Database)

- Ubuntu Live CD by updating Grub resume boot the Boot Menu (Linux)

- KVM QEMU virtual machine installation configuration under CentOS (Linux)

- How to create a cloud encrypted file system in Linux systems (Linux)

- OpenDaylight Helium version installed (Linux)

- Ubuntu Server 14.04 installation Web server (Linux + Apache + MySQL + PHP) (Server)

- Android Studio interface-related settings (Linux)

- Daemon under Linux (Linux)

- MySQL server after an unexpected power outage can not start (Database)

  How to become a better Node.js developers in 2016
  Add Date : 2018-11-21      
  This article discusses some best practices and recommendations Node.js development of these recommendations not only for developers, but also for those who manage and staff to maintain Node.js infrastructure. Following these recommendations provided in this article, you can make better daily development work.

Use ES2015

In the summer of 2015, the final draft (ie ES6) ES2015 officially released. The JavaScript language version adds a number of new language features, including:

Arrow Function
Template strings
rest operator (variable parameters), argument spreading
maps, sets
And many other features. A more complete list of new features that you can learn from Kyle Simpson of the ES6 and Beyond. And most of them feature has been added to the Node.js v4 in.

On the client, you can make use of Babel to use all the new features ES6, Babel is a JavaScript renderer. Currently the server side, we tend to use only those who have been added to the latest stable version of the feature, so without rendering code, which can avoid those potential problems makes us a headache.

For more information ES6 in Node.js, you can visit the official site: https: //nodejs.org/en/docs/es6/

Callback Conventions - while supporting Promise

Last year, we might recommend that you expose the wrong priority for your callback interface module. But with the formal standardization generator function, and asynchronous function is also coming, so we suggest that you write an interface module supports the Promise should expose the wrong priority callback function.

Why do we need this? First callback interface for backward compatibility, in the future in order to be able to get a better compatibility, the need to provide support Promise simultaneously.

You can refer to the following examples to further understanding of how specific should be programmed. In this example, the function reads the package.json readPackage file, and also by Promise and its contents returned callback interface.

const fs = require ( 'fs');

function readPackage (callback) {
  // As of now we do not have default values in Node.js
  callback = callback || function () {}
  return new Promise ((resolve, reject) => {
    fs.readFile ( './ package.json', (err, data) => {
      if (err) {
        reject (err);
        return callback (err);
      resolve (data);
      return callback (null, data);

module.exports.readPackage = readPackage;
Asynchronous mode

In Node.js, a long time you have only two ways to manage asynchronous stream: callback or stream (Stream). For the callback function is concerned, you can use a similar kind of async library for streaming, there are through, bl, highland and other libraries can be selected.

But with Promise, generators, asynchronous functions, and so is gradually introduced into the standard ECMAScript, JS in process control has also been greatly improved.

Asynchronous JavaScript on the history, you can refer to Asynchronous JavaScript development process of this blog.

Error Handling

Error handling plays a crucial role in the application development process: determine the application time of the crash, or simply print an error message, ensure that the application continues to run have a certain degree of difficulty.

In order to illustrate this problem easier, we decided to be divided into two types: programmer errors (programmer errors) and an operational error (operational errors).

Programmer error is what we call the bug, Because you do not know the exact status of the program to run when an error occurs and therefore you had better stop running immediately (crash the process) applications.

On the other hand, the calculation error is a problem because the system itself or due to the remote service. For example: Request timed out and insufficient memory. Based on the characteristics of the error, you can prescribe the right medicine, and then try again, for example, the file is missing, you can go to create the appropriate file.

Error handling in the callback

If an error occurs during the asynchronous operation, asynchronous error object should function as the first argument. You must always check the error object and error handling.

In the case of agreement on callback front which has demonstrated how wrong in the callback function of priority.

Error handling in the Promise

If this is what will happen the following code fragment?

Promise.resolve (() => 'John')
  .then (() => {
    throw new Error ( 'ops');
  .catch ((ex) => {
    console.log (ex);
  .then (() => {
    throw new Error ( 'ups');
    console.log (Doe ');
In the third line will throw an exception.
catch will deal with it, and print out on stdout: [Error: ops]
Execution continues, and line 9 will throw a new error
Nothing really - the last one being thrown error will be silent. You need to pay attention, you should always take a catch statement as the last link in the chain promise. This will solve a lot of your headaches. Like this:

Promise.resolve (() => 'John')
  .then (() => {
    throw new Error ( 'ops');
  .catch ((ex) => {
    console.log (ex);
  .then (() => {
    throw new Error ( 'ups');
    console.log (Doe ');
  .catch ((ex) => {
    console.log (ex);
Now it outputs the following:

[Error: ops]
[Error: ops]
Using JavaScript standard style

In the past few years, we will use JSHint, JSCS, ESLint code quality and other useful tools to automate as much as possible to check our code.

Recently, when it comes to code style, we use the JavaScript standard feross style.

The reason is that it is very simple: without any configuration file, just to put it in the project. It includes the following rules:

Use two spaces as indentation
Use single quotes strings - except to avoid escape
Do not include variables are not used
No semicolon
Never in (or [beginning of the line
Keywords to spaces if (condition) {...}
A space after the function name function name (args) {...}
Always use === instead of == obj == null may be used to check for null || undefined.
Always err function parameters of the process Node.js
Always To add a prefix for the browser window global variables, in addition to document and navigator
Try to avoid using a similar open, length, evet, name and other global variables walk browser.
Of course, if your editor only supports ESLint words, there is a rule base for ESLint using standard style that eslint-plugin-standard. After installing this plugin, you .eslintrc file can be like the following:

    "Plugins": [
12-Factor Applications (The Twelve-Factor Application)

Today, software is usually delivered as a service, they are known as network applications, or software as a service (SaaS). 12-Factor application of the Declaration describes the best practices for Web application development:

Reference Code: a code number, multiple deployment
Dependence: a statement showing dependence
Configuration: storage configuration in the environment
Back-end service: the back-end services as additional resources
Build, release, run: strict separation of building and running
Process: one or more stateless processes running applications
Port Binding: service Port binding
Concurrent: expansion through a process model
Easy handling: quick start and termination of elegance to maximize robustness
Development environment and online environment are equivalent: as much as possible to keep the development, pre-release, the same online environment
Log: the log as an event stream
Management process: the back-end process management tasks as a one-time operation
The theory applies to any language and back-end services (databases, message queues, caches, etc.) to develop applications.

Start a new project

Always start a new project by npm init command. This can create an initial package.json for your project.

If you want to skip directly to the initial question and use the default configuration, you only need to run npm init --yes.

Monitor your application

When a fault occurs or impending failure timely to inform you that you are able to recoup their losses.

For monitoring applications, you can use a similar SaaS products or open source software. In the open-source software, including: Zabbix, Collected, ElasticSearch and Logstash.

If you do not want to deploy your own, consider using online services, you can try to use Trace, it is our company developed Node.js and micro-service monitoring solution.

Use the build system

As automated everything. Nothing should be allowed to do the grunt to allow developers to do more boring and annoying, and this is not only a waste of time, and does not make sense.

Now JvavaScript such tools have been very rich, including Grunt, Gulp, and Webpack, you know a few on the line.

In RisingStack, most of the new front-end development projects are carried out using Webpack automated build other types of use to automate tasks gulp. For starters, Webpack might take a lot of time to understand, so I strongly recommend you to read the Webpack Cookbook.

Using the latest long-term support (LTS) version of Node

In order to obtain better stability and new features, we recommend that you use the latest Node of the LTS (long term support) version, which is an even-numbered release version. Of course, you are free to experiment with the latest version, known as a stable release version using the odd-numbered release.

If you need to work on multiple projects, and using different versions of Node.js, I suggest you best to use a version of Node Manager --nvm.

For more information you can refer to the official website published information Node.js:

What You Should Know about Node.js v5 and More

Weekly update your project depends

Develop your project depends updated weekly habit. In this regard, you can use npm outdated or ncu package.

Select the appropriate database

When we talk about Node.js and database, you may think of the first technique is MongoDB. Of course, this does nothing wrong, but you should not go directly to use it. In doing so before you need to ask yourself and your team a few questions. Including several to the following:

Applications will be structured data?
Applications will be traded handle it?
How long does the data need to be stored?
Maybe you just need Redis, or if you have structured data, then you might want to use is PostgrelSQL. If you need to use SQL in Node.js in, you can look knex.

Using semantic version control (Semantic Versioning)

Version control is a semantic compatibility empty ah rate for the use of three-stage version of the official convention, namely: major.minor.patch, respectively based version, minor version, patch.

If it is not a backward-compatible (backward-compatible) using the API major version number change. When adding new features and API changes are backwards compatible with the use of the minor version number. Bug fix for if only you can use the package version number.

Fortunately, you can use the semantic-release this module automate your JavaScript module release.
- J2EE Example of Filter (Programming)
- Linux server data backup (Server)
- Hadoop 2.2.0 installation development environment (standalone pseudo-distributed mode) (Server)
- Use PuTTY to access the virtual machine Linux under Windows xp (Programming)
- Kubernetes cluster deployment (Server)
- PostgreSQL 9.3.5 database installation under Ubuntu Server 14.04 (Database)
- Linux initialization init systems - Systemd (Linux)
- Java implementation of stacks and queues (Programming)
- CentOS NAT iptables (Linux)
- Ubuntu install snmp packets Unlinked OID in IPATM-IPMC-MIB: marsMIB (Linux)
- Shell for loop (Programming)
- Java development specifications summary (Programming)
- Configure the ASM process on Red Hat Linux 6.5 (Database)
- Five useful commands to manage file types and system time in linux (Linux)
- Java in several ways of using MongoDB (Programming)
- Hyper-V virtual hard disk how to copy files to and attached to the virtual machine (Linux)
- Git Advanced Tutorial (Linux)
- Ubuntu 14.04 virtual machine switching desktop environments (Linux)
- How to Install Sticky Notes on Ubuntu and Derivatives (Linux)
- Oracle 12c PDB Analysis (Database)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.