shell 脚本中的 CoffeeScript —— 由 Apache 运行(作为 CGI)
按照这里的建议我可以获取一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我尝试通过从 CoffeeScript 逆向工程 command.js 并通过以下 JavaScript node.js shell 脚本直接运行我的咖啡脚本(上面的 ctest.cgi)来解决此问题:
我将其放入 /usr/local/bin/drip 中,现在我可以运行 ctest.cgi,只需对顶行进行一些小的更改:
现在,我可以破解 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:
I put this in /usr/local/bin/drip and now I can run ctest.cgi with a small change to the top line:
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.
您可能已经想到了这一点,但是:您是否尝试过
用对
coffee
所在位置的绝对引用替换?env
依赖于PATH
环境变量,而运行./ctest.cgi
时的PATH
则不是必然与 Apache 的相同。You probably already thought of this, but: Have you tried replacing
with an absolute reference to wherever
coffee
is located?env
relies on thePATH
environment variable, and yourPATH
when you run./ctest.cgi
isn't necessarily the same as Apache's.这是我的设置,供任何感兴趣的人使用。
这对性能非常不利!
我没有必要改变我的服务器环境,所以我可以在这里添加我的节点路径。不过,我可以在 .htaccess 中设置一个处理程序:
这意味着我可以作为 CGI 运行并为浏览器提供咖啡:-) 效率很低,但无论如何都没有多少人访问我的网站。
然后我的每个脚本看起来都是这样的......
我知道它不漂亮,但它有效。从脚本运行(尽管承认它不需要是perl!)允许 2>&1 所以我所有的错误都会显示在屏幕上,除非我的标题没有打印......但是 Jared Updike 已经用一个解决了这个问题尝试阻止。
Here is my setup, for anybody that's interested.
It is very bad for performance!
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:
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...
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.
我不知道为什么咖啡失败,但一个可能的(而且非常简单)的解决方案是将您的代码放在单独的文件(test.coffee)中并执行要求:(
在要求咖啡脚本之后,扩展会自动注册)
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:
(after requiring coffee-script the extension is automatically registered)
我尝试更新我的答案,但它被拒绝了,并且一些建议说将其添加为单独的答案...
我刚刚将其更新为带有compile if newer功能的shell脚本:
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: