shell 脚本中的 CoffeeScript —— 由 Apache 运行(作为 CGI)

发布于 2024-10-22 05:32:02 字数 1255 浏览 2 评论 0原文

按照这里的建议我可以获取一个 JavaScript 中的 shell 脚本,该脚本在 node.js 下运行并打印一些 hello world html:

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

为什么这不起作用?

Following the advice here I can get a shell script in JavaScript that runs under node.js and prints a bit of hello world html:

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>");
-------

And running it:

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

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

It also works as expected in Apache and displays the expected HTML in the browser.

Now on to CoffeeScript (note the lovely triple-quoted here docs, Python-style):

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>''');
-------

This works when run locally:

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

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

But not in Apache:

Internal Server Error

Why doesn't this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆躺在深渊里 2024-10-29 05:32:02

我尝试通过从 CoffeeScript 逆向工程 command.js 并通过以下 JavaScript node.js shell 脚本直接运行我的咖啡脚本(上面的 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

现在,我可以破解 CoffeeScript CGI 脚本,只需在浏览器中单击重新加载,而不必在更改 .coffee 文件时手动调用 CoffeeScript 编译器。

I attempted to fix this by reverse engineering command.js from CoffeeScript and running my coffee script (ctest.cgi above) directly, via the following JavaScript node.js shell script:

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});
    });
}
-------

I put this in /usr/local/bin/drip and now I can run ctest.cgi with a small change to the top line:

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

Now I can hack CoffeeScript CGI scripts and simply hit reload in my browser instead of having to manually invoke the CoffeeScript compiler when I change the .coffee files.

莫言歌 2024-10-29 05:32:02

您可能已经想到了这一点,但是:您是否尝试过

#!/usr/bin/env coffee

用对 coffee 所在位置的绝对引用替换? env 依赖于 PATH 环境变量,而运行 ./ctest.cgi 时的 PATH 则不是必然与 Apache 的相同。

You probably already thought of this, but: Have you tried replacing

#!/usr/bin/env coffee

with an absolute reference to wherever coffee is located? env relies on the PATH environment variable, and your PATH when you run ./ctest.cgi isn't necessarily the same as Apache's.

谁的新欢旧爱 2024-10-29 05:32:02

这是我的设置,供任何感兴趣的人使用。

这对性能非常不利!

~/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/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 已经用一个解决了这个问题尝试阻止。

Here is my setup, for anybody that's interested.

It is very bad for performance!

~/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 `;
------

I don't have the necessaries to alter my server environment, so I get to add my node paths here. However, I can set up a handler in .htaccess:

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

This means I can run literate as CGI and serve up coffee for the browser :-) Very inefficient, but no many people are coming to my website anyway.

Then each of my scripts looks something like this...

~/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"
        ]
    ]
------

I know it's not pretty, but it works. And running from a script (although admittedly it needn't be perl!) allows 2>&1 so all my errors get to the screen, unless my header isn't printed.... but Jared Updike already solved that with a try block.

浮华 2024-10-29 05:32:02

我不知道为什么咖啡失败,但一个可能的(而且非常简单)的解决方案是将您的代码放在单独的文件(test.coffee)中并执行要求:(

#!/usr/local/bin/node

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

在要求咖啡脚本之后,扩展会自动注册)

I have no idea why coffee fails, but a possible (and very simple) solution is to put your code in a separate file (test.coffee) and do a require:

#!/usr/local/bin/node

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

(after requiring coffee-script the extension is automatically registered)

樱娆 2024-10-29 05:32:02

我尝试更新我的答案,但它被拒绝了,并且一些建议说将其添加为单独的答案...

我刚刚将其更新为带有compile if newer功能的shell脚本:

#!/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

I tried to update my answer, but it got rejected and some advice said to add this as a separate answer...

I just updated that to a shell script with compile if newer functionality:

#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文