Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Created January 1, 2017 04:48
Show Gist options
  • Save ScalaWilliam/e0b7257175a1bdf6c5da6025b4329f43 to your computer and use it in GitHub Desktop.
Save ScalaWilliam/e0b7257175a1bdf6c5da6025b4329f43 to your computer and use it in GitHub Desktop.
import com.eclipsesource.v8.JavaCallback;
import com.eclipsesource.v8.NodeJS;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Mapp {
private static String NODE_SCRIPT = "var http = require('http');\n"
+ ""
+ "var server = http.createServer(function (request, response) {\n"
+ " response.writeHead(200, {'Content-Type': 'text/plain'});\n"
+ " response.end(someJavaMethod());\n"
+ "});\n"
+ ""
+ "server.listen(8000);\n"
+ "console.log('Server running at http://127.0.0.1:8000/');";
public static void main(String[] args) throws IOException {
final NodeJS nodeJS = NodeJS.createNodeJS();
JavaCallback callback = (receiver, parameters) -> "Hello, JavaWorld!";
nodeJS.getRuntime().registerJavaMethod(callback, "someJavaMethod");
Path f = Paths.get("meh.js").toAbsolutePath();
Files.write(f.toAbsolutePath(), NODE_SCRIPT.getBytes());
nodeJS.exec(f.toFile());
while (nodeJS.isRunning()) {
nodeJS.handleMessage();
}
nodeJS.release();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment