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:
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:
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:
However, for large projects, it is not good to put source files and compilation result files together. Just specify an output directory:
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:
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.