我有一個需要運行節點腳本的rails應用程序。我想使用ExecJS gem是從Rails應用程序運行js的最簡單的方法。但是,迄今爲止,ExecJS已被證明使用起來非常令人沮喪。使用ExecJS從Rails應用程序調用Node.js腳本
這裏是我需要運行該腳本:
// 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)
從我的鐵軌控制檯,我試試這個:
[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'
注意,我可以成功地使用「節點test.js運行此腳本'我也能運行使用反斜槓語法的腳本使用ruby提供的反向語法:
`node test.js`
但是,感覺就像一個hac k ...
任何接受者?
聽起來像一個偉大的使用案例微服務做類似的東西。有一個Node.js服務器參加Rails服務器發出的請求! – vemv
這與問題無關,但與代碼相關。如果你想從rails應用程序創建pdf,爲什麼不使用像'prawn'這樣的ruby庫? – hamitron