How to expose a javascript logic which is currently there in a legacy system as a webservice?
Only way to do this is convert javascipt code to a another language right? Im finiding a way of expose the javascript code directly as a webservice because it has thousand of codes. So convert those to a java or other language is a huge effort.
You need to put some sort of wrapper around the JS that can take input and output via HTTP and call the appropriate JS functions.
You could write the entire thing using node.js or you could use another language which has an interface to a JS engine such as Perl or Java.
Related
I am creating a test case website to allow students to solve some programming questions and test their code. I am using express and Node Js.
I am planning to add multiple languages such as Javascript, Python, and Java.
For javascript, I was able to use vm2 sandbox to safely run the user's arbitrary code. Go here to see how I did it.
However, I am still not sure about how to implement it for Java and Python.
For Python, there is the build-in "child_process" that allows running a python script which is basically another python file and allows us to get the output from it. What I want is to get the user's code from a text area and run it inside the app.js file.
Is there any library or sandbox that allows me to do that?
I need to call JavaScript library on server side, which result will be returned to the user through Java.
This library uses many external JavaScript library, so it's not a single .js file.
Couple of options, but without more context, we can only guess.
Nashorn/GraalVM to execute Javascript from Java, but it depends on the library you're trying to invoke
Install Node.js on the server, write a Node.js script which uses that lib and invoke the script from Java. Depending on the complexity of the use case and how often you're trying to do this, maybe write a microservice in Node.js and call it from Java via REST.
I went through below article
https://medium.freecodecamp.org/get-started-with-webassembly-using-only-14-lines-of-javascript-b37b6aaca1e4
and very impressed that we can use c++ code in javascript using web assembly.
Do we have any option to create such web assembly using c# where we can create web assembly and use in javascript like angular or react.
went through
https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/blazor/get-started?view=aspnetcore-3.0&tabs=visual-studio
But that does not look like creating an assembly that can be used in a separate angular only project with by importing
Thanks
From msdn:
JavaScript interop
For apps that require third-party JavaScript libraries and browser APIs, Blazor interoperates with JavaScript. Components are capable of using any library or API that JavaScript is able to use. C# code can call into JavaScript code, and JavaScript code can call into C# code. For more information, see JavaScript interop.
[https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/blazor/?view=aspnetcore-3.0][1]
Ithink looking to Blazor source code can help you to.
I think what you're looking for isn't Blazor, because it's a complete UI framework for ASP.NET client/server stuff using SignalR, that uses WASM at the client side. Maybe too much for your purposes, even if you're able to use any JavaScript framework together with Blazor, too.
But you're looking for a simple way to create just a WASM that exports methods to JavaScript that you can write using C#, right? Well, then I suggest you to have a look here:
https://itnext.io/run-c-natively-in-the-browser-through-the-web-assembly-via-mono-wasm-60f3d55dd05a
It seems the Mono way is working as you'd expect: You write methods in C#, compile a WASM and then you're ready to load and call them from any JavaScript client app, and you don't have to deal with ASP.NET stuff at all.
Compared to a WASM that has been created using lower level C++, you'll have a big bunch of DLLs for the Mono runtime, that need to be loaded to the client browser (!). That's a huge overhead, if you plan only a small feature set to be exported by the WASM. The best argument for creating WASM using Mono for me is, that I can use my existing codebase with all the algorithms and business logic for a really complex app, so I don't have to write and maintenance the same code twice in different languages.
I'm trying to write a module of code which can be used with Javascript for the client side browser and Kotlin for a Java desktop application and Android app.
The main logic manipulates a bitmap/png file.
Is there a way which I can write an interface to be used and have different implementations of the interface for JS and Kotlin?
For example, write a wrapper class for an image (load from image, set pixels, get pixels) in Java (using BufferedImage) and JS (using Canvas)?
I'm new here so if anything doesn't make sense or needs more clarification, please let me know!
Yes, it's certainly possible to do that. This answer is a generic Java and JavaScript answer; there may also be a Kotlin-specific approach, see the link in zsmb13's comment. But the generic Java and JavaScript approach would be:
Write your interface around BufferedImage in Java
Write your interface around canvas in JavaScript
Write your manipulation code using that interface in JavaScript
On the browser, your manipulation code would run directly on the browser's JavaScript engine
In the Java app, you'd run your JavaScript code using javax.script to run it in Nashorn (or Rhino on older Java environments).
Whether it's the best solution to your problem is something only you can decide, but yes, you can do it.
I'm creating a video game that has single player and multiplayer. The single player is done in C# (Unity), and want to use Gamesparks BaaS that works on Javascript (NodeJS) for their server code.
Since I want the multiplayer server to be authoritative, I need the server code to run Javascript and the client will run C#, which means duplicated code
But, if I can create .NET dlls in Javascript I can reduce code duplication heavily by using those libraries in Unity and using the Javascript code on the server.
Thanks!
If you want to use javascript (and by javascript I mean nodejs), along with c# and communicate between those two, you can use electron-edge. It helps you to run c# code using nodejs through Electron API.
Here is the link of their Github url: Electron-Edge-documentation
You can also interact with dlls, using an creating your application as an electron app. You refer this question where you will find some more details regarding it.