node. js Best Practices-part 2

Source: Internet
Author: User

Original: https://blog.risingstack.com/node-js-best-practices-part-2/

Unified Style

It is necessary to create a style guide for a large team to develop JS applications. It is recommended to look at this node. js Style Guide.

Jscs is a JS code style checker. The installation commands are as follows:

NPM Install Jscs--save-dev  

Next, add the custom script in the Package.json file:

Scripts: {      "Jscs": "Jscs Index.js"}

Of course, you can add multiple files/folders.

Enforce Jshint/jscs Rules

Checking the code style before committing commits is a good choice.

This is easy to do, just use Pre-commit:

NPM Install--save-dev Pre-commit  

Configure your package.json file as follows:

Pre-commit ": [      " Jshint ",    " Jscs "],

Before each commit, Pre-commit will find the corresponding script and run it.

JS over JSON for configuration

Many of the project's configuration files are JSON. We recommend that you also use the Config.js file:

Use Node_path

Have you ever met the following things?

When you accept the structure of a large complex project, you will find that the dependency module is very confusing. You can use the following methods to solve this problem:

    • Point your moudle to the Node_modules folder
    • UseNODE_PATH

Risingstack use Node_path this way.

Setting up Node_path

Imagine the following project structure:

We can use NODE_PATH指向lib文件夹 . The Start Script section in our Package.json file can be set so that we can run our app with the NPM start command.

Dependency Injection

Dependency injection is very useful for testing

node. js Best Practices-part 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.