2

こちらのアドバイスに従って、node.js で実行され、hello world html を少し出力する JavaScript のシェル スクリプトを取得できます。

test.cgi
-------
#!/usr/local/bin/node

console.log("Content-Type: text/html; charset=utf-8\n");
console.log("<html><body><h1>hello node.js world</h1></body></html>");
-------

そしてそれを実行します:

$ ./test.cgi
Content-Type: text/html; charset=utf-8

<html><body><h1>hello node.js world</h1></body></html>

また、Apache でも期待どおりに動作し、期待される HTML をブラウザーに表示します。

それでは、CoffeeScript に進みます (素敵な三重引用符で囲まれたこちらのドキュメント、Python スタイルに注意してください):

ctest.cgi
-------
#!/usr/bin/env coffee

console.log("Content-Type: text/html; charset=utf-8\n");
console.log('''<html><body><h1>hello CoffeeScript world
</h1></body></html>''');
-------

これは、ローカルで実行すると機能します。

$ ./ctest.cgi
Content-Type: text/html; charset=utf-8

<html><body><h1>hello CoffeeScript world
</h1></body></html>

しかし、Apache にはありません:

Internal Server Error

なぜこれが機能しないのですか?

4

5 に答える 5

3

これを解決しようと、CoffeeScript から command.js をリバース エンジニアリングし、次の JavaScript node.js シェル スクリプトを使用して、コーヒー スクリプト (上記の ctest.cgi) を直接実行しました。

drip
-------
#!/usr/local/bin/node

var fs = require('fs');
var cs = require('/usr/local/lib/coffee-script/lib/coffee-script');
var args = process.argv;
var fname = args[2];
if (fname)
{
    fs.readFile(fname, function (err, data) {
      if (err) throw err;
      var text = data.toString(); //console.log(data.toString());
      cs.run(text, {'filename':fname, 'bare':undefined});
    });
}
-------

これを /usr/local/bin/drip に置き、ctest.cgi を実行して、一番上の行を少し変更します。

#!/usr/bin/env /usr/local/bin/drip

今では、.coffee ファイルを変更するときに CoffeeScript コンパイラーを手動で呼び出す代わりに、CoffeeScript CGI スクリプトをハックしてブラウザーでリロードを押すだけで済みます。

于 2011-03-13T04:41:10.467 に答える
2

あなたはおそらくすでにこれを考えていましたが、

#!/usr/bin/env coffee

どこにあるのかを絶対的に参照していcoffeeますか?は環境変数にenv依存しており、実行時は必ずしも Apache のものと同じではありません。PATHPATH./ctest.cgi

于 2011-03-13T15:38:04.777 に答える
1

興味のある人のために、これが私のセットアップです。

パフォーマンスに非常に悪いです!

~/coffee
------
#!/usr/bin/perl 

# this is coffee runner!

print ` PATH="\$PATH:~/www/cgi-bin/bin" ; ~/www/cgi-bin/bin/node_modules/coffee-script/bin/coffee $ARGV[0] 2>&1 `;
------

サーバー環境を変更する必要がないので、ここにノード パスを追加します。ただし、.htaccess でハンドラーを設定できます。

~/dir/.htaccess
------
AddHandler cgi-script .litcoffee
DirectoryIndex cv.litcoffee
------

これは、私が CGI として読み書きできることを意味し、ブラウザーにコーヒーを提供できます :-) 非常に非効率的ですが、とにかく多くの人が私の Web サイトに来ません。

次に、各スクリプトは次のようになります...

~/web/ascript.litcoffee
------
#!/usr/bin/perl /home/jimi/coffee

This is literate coffeescript!

    module.paths.push "/home/jimi/www/cgi-bin/bin/node_modules"
    require "coffee-script"

This is a wee module I wrote for spewing tags, with content and attributes

    global[k.toUpperCase()] = v for k,v of require './html.litcoffee'

It also provides a header function, but I'm going to move that to a CGI module when I get around to it.

    console.log CGI_HEADER()

Now we can put something to the browser.

    console.log HTML [
        HEAD [
            META {charset:"utf-8"}
            SCRIPT [],
                src : "https://raw.github.com/jashkenas/coffee-script/master/extras/coffee-script.js"
            SCRIPT [],
                src : "runsonclient.coffee"
                type    : "text/coffeescript"
            LINK
                rel : "stylesheet"
                href    : "mystyles.css"
            TITLE "A page title"
        ]
        BODY [
            H1 "a page title"
            INPUT 
                id     : "myinput"
                type   : "text"
            SVG
                id     : "mysvg"
                width  : "80%"
                height : "20"
            DIV
                id     : "mydiv"
        ]
    ]
------

私はそれがきれいではないことを知っていますが、うまくいきます。そして、スクリプトから実行すると (確かに perl である必要はありません!) 2>&1 が許可されるため、ヘッダーが印刷されない限り、すべてのエラーが画面に表示されます....しかし、Jared Updike は try ブロックで既にそれを解決しています.

于 2013-11-08T02:11:57.737 に答える
0

回答を更新しようとしましたが、拒否され、これを別の回答として追加するようにというアドバイスがありました...

それを、より新しい機能があればコンパイルするシェル スクリプトに更​​新しました。

#!/bin/sh
CS=$1        # the coffeescript to be run
# the following works because (my) apache runs in the script's directory and passes just the filename (no path)
# if would be safer to test that this is the case, or make sure the dot is added to the filename part
JS=.$CS.cjs  # the compiled javascript
CE=.$CS.cerr # compiler errors
PATH="$PATH:/home/jimi/www/cgi-bin/bin:/home/jimi/www/cgi-bin/bin/node_modules/coffee-script/bin"
if [ ! -f $JS ] || [ $CS -nt $JS ] ; then
    coffee -c -p $1>$JS 2>$CE
fi
node $JS 2>&1   # runtime errors to browser
于 2016-10-25T10:53:34.987 に答える
0

コーヒーが失敗する理由はわかりませんが、考えられる (そして非常に単純な) 解決策は、コードを別のファイル (test.coffee) に入れて、require を実行することです。

#!/usr/local/bin/node

require('coffee-script')
require('./test')

(coffee-script を要求した後、拡張子は自動的に登録されます)

于 2011-03-14T04:36:32.923 に答える