使用ExecJS从Rails应用程序调用Node.js脚本 [英] Calling Node.js script from Rails app using ExecJS

查看:306
本文介绍了使用ExecJS从Rails应用程序调用Node.js脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要运行节点脚本的rails应用程序。我想使用ExecJS gem是从rails应用程序运行js最干净的方法。但是,到目前为止,ExecJS使用起来非常令人沮丧。

I have a rails application that needs to run a node script. i imagine that using the ExecJS gem is the cleanest way to run js from a rails app. However, so far, ExecJS has proved to be very frustrating to use.

这里是我需要运行的脚本:

here is the script I need to run:

// Generated by CoffeeScript 1.7.1
(function() {
  var PDFDocument, doc, fs;
  fs = require("fs");
  PDFDocument = require('pdfkit');
  doc = new PDFDocument;
  doc.pipe(fs.createWriteStream('output.pdf'));
  doc.addPage().fontSize(25).text('Here is some vector graphics...', 100, 100);
  doc.save().moveTo(100, 150).lineTo(100, 250).lineTo(200, 250).fill("#FF3300");
  doc.scale(0.6).translate(470, -380).path('M 250,75 L 323,301 131,161 369,161 177,301 z').fill('red', 'even-odd').restore();
  doc.addPage().fillColor("blue").text('Here is a link!', 100, 100).underline(100, 100, 160, 27, {
    color: "#0000FF"
  }).link(100, 100, 160, 27, 'http://google.com/');
  doc.end();
}).call(this)

从我的rails控制台,我这个:

From my rails console, I try this:

[2] pry(main)> file = File.open('test.js').read
[3] pry(main)> ExecJS.eval(file)
ExecJS::ProgramError: TypeError: undefined is not a function
from /Users/matt/.rvm/gems/ruby-2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:68:in `extract_result'

请注意,我可以运行此脚本成功使用'node test.js',我也可以使用ruby提供的反引号语法运行脚本:

Note that I can run this script successfully using 'node test.js' and I am also able to run run the script using the backtick syntax ruby offers:

`node test.js`

但这感觉就像是黑客......

But that feels like a hack...

任何接受者?

推荐答案

这是错误的,因为EvalJS不支持require()。 'require'未定义,undefined不是函数。 ;)

It's erroring out because require() is not supported by EvalJS. 'require' is undefined, and undefined is not a function. ;)

这篇关于使用ExecJS从Rails应用程序调用Node.js脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆