2014-04-03 56 views
0

你好railites和jsheads,最简单的方法Rails应用程序

我是一个Ruby的家伙,漂亮的新节点的事情,但我学习,到目前为止,我真的很喜欢它。我在heroku上开发了一周左右的rails应用程序,并且(当然)需求已经发展。

我需要能够从我的Rails应用程序中使用pdfkit,一个用于创建PDF的节点库。

最终,我是在完成这个最简单的方法之后。这是我迄今为止尝试过的:

从heroku文档here,我知道heroku rails应用程序带有节点js运行时,但似乎npm不包括在内,我找不到明显的方法要求pdfkit。

所以我跟着这个blog的领先优势,采用ddollar的多buildpack:

heroku config:add BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-multi.git 

这里有段形成相关的文件(让我知道如果我失去了一些东西;))

。 buildpack

https://github.com/heroku/heroku-buildpack-nodejs 
https://github.com/heroku/heroku-buildpack-ruby 

的package.json

{ 
    "name": "MoneyMaker", 
    "version": "0.0.1", 
    "dependencies": { 
    "pdfkit": "0.5.2" 
    }, 
    "repository": { 
    "type" : "git", 
    "url" : "https://github.com/mattwalters/example.git" 
    } 
} 

的Gemfile:

... 
gem 'execjs' 
... 

test.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) 

注意我做了检查node_modules到源代码控制。 我把所有这一切的Heroku,并试图手动调用test.js与execjs:

matt$ heroku run rails c 
heroku-irb> ExecJS.eval(File.open('test.js').read) 

我得到folloring错误:

ExecJS::ProgramError: TypeError: undefined is not a function 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:68:in `extract_result' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:28:in `block in exec' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:41:in `compile_to_tempfile' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:27:in `exec' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/external_runtime.rb:19:in `eval' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/runtime.rb:40:in `eval' 
    from /app/vendor/bundle/ruby/2.1.0/gems/execjs-2.0.2/lib/execjs/module.rb:23:in `eval' 
    from (irb):3 
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start' 
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start' 
    from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>' 
    from /app/bin/rails:4:in `require' 
    from /app/bin/rails:4:in `<main>' 

我试图简化和只运行:

heroku - irb> ExecJS.eval("require('pdfkit')") 

但我得到了同样的错误。当涉及到execjs时,我想我错过了一些非常重要的东西。任何人都可以启发我吗?

然后我想看看我是否可以直接从节点调用test.js。注意这可以在我的本地开发环境中使用。

matt$ heroku run bash 
heroku$ node test.js 

,但我得到:

node.js:134 
     throw e; // process.nextTick error, or 'error' event on first tick 
     ^
Error: Cannot find module 'zlib' 
    at Function._resolveFilename (module.js:320:11) 
    at Function._load (module.js:266:25) 
    at require (module.js:348:19) 
    at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:12:10) 
    at Object.<anonymous> (/app/node_modules/pdfkit/js/reference.js:101:4) 
    at Module._compile (module.js:404:26) 
    at Object..js (module.js:410:10) 
    at Module.load (module.js:336:31) 
    at Function._load (module.js:297:12) 
    at require (module.js:348:19) 

所以现在我要把我的手,并希望社会各界的智慧可以帮助我。如果您需要更多信息,请告诉我。

非常感谢, 马特

回答

1

所以问题是,默认buildpack自动安装一个版本的节点,如果你的应用程序依赖于execjs。不幸的是,这是一个非常旧的节点版本 - 0.4.7 - 与我需要使用的节点库不兼容。

我的解决方案是分叉heroku的buildpack并修补add_node_js_binary方法从不安装节点二进制文件。只要您的多buildpack配置包含一个节点发行版,就可以工作。看看这里:

https://github.com/mattwalters/heroku-buildpack-ruby/blob/master/lib/language_pack/ruby.rb

+0

真棒,我有这个确切的问题。正如您所说,/ app/bin首先位于环境PATH变量中,它具有节点0.4.7,而PATH后面的另一个目录具有在package.json中指定的节点版本。超级讨厌 - 去分叉你的回购,并开心。谢谢! – Cymen

+0

非常欢迎! –

+0

顺便说一句,我仍然有麻烦使ExecJS行为....看到我这里的问题在这里:http://stackoverflow.com/q/22852086/1396827 有任何想法吗? –

相关问题