node.js - 如何从 scala.js 调用 nodejs 模块?

标签 node.js scala.js

我正在尝试使用 scala.js + nw.js编写一些应用程序,并将使用 scala.js 中的一些 Node 模块。但我不知道该怎么做。

说,有模块 fs 我可以用 Javascript 编写这样的代码:

var fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
}); 

但是如何从头开始在 scala.js 中做同样的事情呢?

最佳答案

使用 js.Dynamicjs.DynamicImplits (另见 a longer answer on the topic ),您可以在 Scala.js 中音译您的代码:

import scala.scalajs.js
import js.Dynamic.{global => g}
import js.DynamicImplicits._

val fs = g.require("fs")
fs.writeFile("/tmp/test", "Hey there!", { (err: js.Dynamic) =>
  if (err)
    console.log(err)
  else
    console.log("The file was saved!")
})

您可以在此处使用 Scala.js 中的 Node.js fs 模块找到更长的源代码: https://github.com/scala-js/scala-js/blob/v0.6.0/tools/js/src/main/scala/org/scalajs/core/tools/io/NodeVirtualFiles.scala

关于node.js - 如何从 scala.js 调用 nodejs 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28656343/

相关文章:

node.js - SocketIO + MySQL 认证

php - 如何使用 `npm` 在 PHP 中执行使用 `exec()` 命令安装的命令?

future - 在 Scala.js 中沉睡在 Future 中

scala - Scala.js 中 js 和 scala 函数之间的互操作

scala - Scala.js 中 JS 库的类型化外观

windows - NPM Windows 路径问题

javascript - 参数 "options"不是 Firestore 上的有效 SetOptions 错误

javascript - 使用 Webpack 4 减少和优化具有两个入口点的 bundle

scala - 无法在 PhantomJSEnv 上运行 scala.js,requiresDOM 设置强制为 false

scala - 单元测试 Scala.js : Read test data from file residing in `test/resources`