Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use "coffee" instead of "node" command in production

I have an app.js that is running express.js.

I wanna convert the code to coffeescript and thought about to create a app.coffee that I compile to app.js so I can run it with "node app.js".

But then it hit me that I could just write that file in app.coffee and run it with "coffee app.coffee".

Is this a better way? Can I run the server with "coffee" in production?

like image 983
ajsie Avatar asked Sep 29 '11 10:09

ajsie


People also ask

What is the command to run the node program?

Type node followed by the name of the application, which is test-node. js in this case, and then press Enter . The result of running the application will be printed out to the command prompt.

What is JS coffee file?

CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way. The golden rule of CoffeeScript is: “It's just JavaScript.” The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.


2 Answers

Yes you can use coffee command in production. I use it.

I can see two reasons why you would want to use app.js wrapper.

  1. You want to use local installation of CoffeeScript. (different versions between apps)
  2. You want to use the default npm start to launch your server :) See npm help scripts

Oh, and you don't need compile it. You can use a wrapper like this which compiles the coffee file transparently:

server.js:

require('coffee-script').register();
require("./yourcoffeeapp.coffee");

This wrapper technique is especially useful if you want to use CoffeeScript in some hosted environments that does not directly support the CoffeeScript command. Such as Cloud 9 IDE. No need to fiddle with compiled js-files.

like image 195
Epeli Avatar answered Oct 05 '22 04:10

Epeli


I upvoted Epeli's answer, which is clear and excellent—using a .js "wrapper" rather than the coffee command saves you from potential path headaches—but since this is a subjective question, let me throw in a contrary opinion.

Many CoffeeScripters, myself included, recommend compiling non-trivial Node apps to JS before deployment. It's not hard—look at Sam Stephenson's node-coffee-project template, which includes a Cakefile that makes compiling and testing a breeze.

One major reason for doing this is that Node stack traces give line numbers that refer to the compiled JavaScript, not the original CoffeeScript. So when errors are recorded in your server logs, it's nice to be able to look at the corresponding code right on the server.

Another advantage to compiling to JS is that it lets you work with more tools on the server—many Node debuggers, testing frameworks, and amazing goodies like cluster like to operate directly on .js files.

Getting a good compilation setup for your project takes some work, but I think you'll find it worthwhile.

like image 37
Trevor Burnham Avatar answered Oct 05 '22 04:10

Trevor Burnham