Running jsdom in Nashorn

2019-04-13 03:59发布

I want to render d3 charts on the server. I had 3 options: Node, Phantom, and Nashorn.

I prefer Nashorn because my API is Scala Play and I don't want to manage another process. (deployment, load, queue, etc etc)

So now I need to get JSDom working in Nashorn, so that D3 will have something to render to.

This works so far but I can't figure out how to add jsdom

class Application @Inject() (val messagesApi: MessagesApi) extends api.ApiController {

  def test = ApiAction { implicit request =>
    ok("The API is ready")
  }

  def pptx = Action { implicit request =>
    val manager: ScriptEngineManager = new ScriptEngineManager
    val engine: ScriptEngine = manager.getEngineByName("nashorn")
    engine.eval(new FileReader(Play.getFile("/ext/lodash.js")))
    val output = engine.eval("function hello(){return _.join('Hello world nashorn does this thing'.split(' '), '-');} hello();")
    Ok(output.toString)
  }
}

enter image description here

1条回答
狗以群分
2楼-- · 2019-04-13 04:22

I'm loading a fork of domino in nashorn to run d3. Domino is a server-side DOM implementation based on Mozilla's dom.js. I loaded it using r.js from require.js. You can use this readfully function that is missing from nashorn to get require.js working.

查看更多
登录 后发表回答