Home >Web Front-end >JS Tutorial >Summary of methods for writing node.js projects using coffeescript_javascript skills

Summary of methods for writing node.js projects using coffeescript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:47:051089browse

Node.js writes applications based on JavaScript, which is my main development language. CoffeeScript is a programming language compiled to JavaScript. In fact, the CoffeeScript language is also very flexible to use because of its one-to-one translation into JavaScript. There are many ways to introduce it into the project. Here, I will summarize the methods of using coffeescript to write node.js projects.

Directly use the coffee command to run pure coffeescript projects
Generally speaking, when you mention coffeescript, you will naturally think of it as the younger brother of javascript, and it can never escape the shadow of js. In fact, you can think of it as an independent language. We all know that after installing the coffee-script package globally on the node platform, you can enter the coffeescript interactive interface through the coffee command. You can also call it repl. If your project is completely written in coffee, it is simple. Just use the coffee command directly on your entry script. For example, if your entry script is named "app.coffee", then execute:

Copy code The code is as follows:

coffee app.coffee

Note that the extension coffee here cannot be omitted.

This method should be said to be the most "official" way to use coffeescript. Simple and direct! Moreover, once you use a coffee file as the entry point of the project, the entire project will be compatible with both coffee and js. You can require any js or coffee files and modules in the project, and you can even require coffee files in the js files in the project. And when you reference whether it is a coffee or js file, there is no need for an extension, as long as the previous part of the name does not conflict.

The biggest problem with this method is that if it is used as a module, it can only be used for coffee projects; if it is used as an application, coffee-script must be installed in the running environment. After all, coffeescript is still a niche language, and it is a pity that it lost js users when it was used as a module.

Another possible shortcoming is performance. After all, there is only js engine in node. The coffee code needs to be compiled into js before running. This process takes a little time, although the compilation speed from coffee to js is actually quite fast. Fast. But this shouldn’t be a big problem. Generally speaking, require is written at the top of the file, that is, when the application starts, it will require all the required files at once. When requiring, coffee will be compiled into js. If it is placed in the js engine, the time consumed in compilation will be concentrated when the application is started, and there will hardly be a requirement for new coffee during runtime. The most common usage scenario of node is web server, which is no problem.

Reference coffeescript in javascript project
coffee-script in npm can be installed globally or as a module of the project. So what is the significance of coffee-script as a module of the project? In fact, a coffeescript compiler is added to the project, and the project can compile coffee files at any time during runtime.

You definitely want to reference the coffee file casually like in the first method. No problem, just register. If your project entry file is app.js, then you only need to add this sentence at the beginning of the file:

Copy code The code is as follows:

require('coffee-script/register');

Then you can require the coffee file at will in the project.

This method is essentially the same as the first method, except that coffee-script is not installed globally, so your module can exist independently. As an application, coffee-script does not need to be installed in the environment.

Disadvantages, I think the biggest problem is that it is easy to mess up the code, sometimes js, sometimes coffee, of course the first method may also be like this, but if you use coffee to start it, there should be no js written in it... In short, I think it is better to unify the languages ​​​​for a project (unfortunately, I mainly use this method. In a project that has already written the general structure in js, I just want to use coffee...)

The performance issue is the same as the first method, so I won’t say more.

The orthodox way - compilation
When I talk about compilation, I feel like I am back in the era of serious C or Java. Indeed, as a compiled language, compiling and then running is the right way to go. c has gcc, java has javac, and cofee has coffee -c.

It is very simple to compile a coffee file. For example, if you want to edit the app.coffee file, just execute in the current directory of the file:

Copy code The code is as follows:

coffee -c app.coffee

A file named app.js appears in the current directory. This command can also be applied to directories. For example, if you put all the coffee source files in the project in the src directory, then execute:

Copy code The code is as follows:

coffee -c src

All coffee source files in the src directory and its subdirectories at all levels will be compiled into js files and placed in the same directory as the source files.

However, for large projects, it is not good to put source files and compilation result files together. Just specify an output directory:

Copy code The code is as follows:

coffee -c -o outputs src

The order of parameters of this directive is a bit strange. This is how it is defined in coffee’s help:

Copy code The code is as follows:

coffee [options] path/to/script.coffee -- [args]

Note that all options are between coffee and the file path. The last args are the parameters passed when executing the target file as a script. In other words, all options can be placed between coffee and the file name. The -c option is independent and does not have its own parameters. It only means that the file provided at the end of the instruction is to be compiled, so it can be written like this:

Copy code The code is as follows:

coffee -o outputs -c src

If you want to add an option so that the compilation result is not surrounded by self-executing function bodies, it is:

Copy code The code is as follows:

coffee -o outputs -c -b src

If you want to compile all source files into a target file named out.js, that is:

Copy code The code is as follows:

coffee -o outputs -c -j out src

It would be quite annoying if you have to execute instructions like this every time you change some code. The coffee command has an option -w that can monitor source file changes and automatically compile:

Copy code The code is as follows:

coffee -o outputs -c -w src

For large-scale projects, it is best to determine the compilation method in advance, so that all developers can handle all compilation matters with only one command, which requires automated construction.

offee provides an automated build tool, cake, which is like make in the c world. But as stated on the official website, cake is a very simple build system. In fact, the function of cake is to execute a script named cakefile, and the cakefile script is written in coffeescript. This script only provides very limited built-in functions, such as task, which are used to declare an instruction and its corresponding description and execution functions. The other thing is to write a pure node project. To complete the compilation, you must either use the fs module of node to output the string compiled by the coffee module, or use the child_process module to execute shell instructions. In fact, the target of cake construction does not necessarily have to be coffee, because it actually executes a node script and can handle any automated things.

In addition, there are some better third-party automated construction tools that can also complete the automatic compilation of coffee, such as the famous Grunt, and the domestic fekit, etc.

This orthodox compilation method may be the most reliable and should be loved by experienced programmers. It allows the team to form a fixed development model. In addition, the compiled project becomes a pure js project, and no additional dependencies are required whether it is run directly as an application or referenced by other projects as a module. And there is no need to compile at runtime, so there is no performance problem caused by compilation.

The disadvantage is that it is too troublesome. If you are doing a small project, it will take half a day just to create the cakefile or configure grunt, which is not worth it.

Based on the above summary, it is actually very simple to use coffeescript to write node.js projects. Next, I hope everyone will hurry up and use coffee. At the same time, I hope the above content will be helpful to everyone.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn