How run Javascript on server side in Ruby on Rails - javascript

I intend to program a Bitcoin related Web application with Ruby on Rails.
Now there are some very useful libraries out there like Bitcoinjs or Bitcore that are written in Javascript and can be run on the client side in the Webbrowser, or server side with Node.js. However, since I intend to program this application with RoR, I wonder how I can take advantage of those libraries on the server side.
Is there a way (i.e. a gem) that allows interpreting Javascript on the server or can I start a Node.js instance in my RoR code by which I can then interpret those javascript libraries and execute their functions?
Any help is greatly appreciated.

Since it seams like you are very determined to use these nodejs libraries, here is how I would go about doing it. Since it is a very bad idea to run code in the wrong environment, I would instead make a setup like this.
Front end, ERB template => RoR server => nodeJs server
The idea behind this setup is that RoR is more like a task master and nodeJs is the worker. Your RoR app would figure out what needs to happen and then tell nodeJs to do it. Nodejs would then respond with the result which RoR would present back to the user. Here is an example:
Someone on the front end asks you app to transfer x bitcoins to someone else. Your Ruby on Rails app would then do whatever non bitcoin tasks were needed(ex: logging in, messaging people). Then your RoR app would send a request to the nodeJs server which would actually do the bitcoin tasks.
Here is how you would go about implimenting this setup.
Make the front end
Connect frontend actions with your RoR controllers
Make a RESTful HTTP API on your nodeJs.
If you don't already know what a RESTful API is, research it. It is a whole separate topic
Make an entry point for each action you want your app to perform.
Make this nodeJs server run localy, so people can not access it without going through your RoR app
Then use something like restful-client to use you nodeJs API from your RoR app

Related

Can I create web api using express.js and not have node.js installed?

I am currently in the process of creating a portfolio website for myself but due to hosting restrictions, I cannot make use of Node.js.
I know Angular can run on any web server, but is it possible to make use of Express.js to create web api's with relying on Node.js to run these web api's using Express.js?
If not, is there an alternative solution to create web api's that I can call using Angular and later for my mobile version of my website?
Please note that my shared hosting runs using cPanel.
As per definition Express.js, or simply Express, is a web application framework for Node.js so you can't do that. Alternatives would be to use a different backend language.
That also depends if your server supports them, for example, you can go with .NET CORE
You cannot use Express without NodeJS by definition so you have to deploy your backend somewhere else in you want to use it.
I suggest giving a look Firebase: you could write your backend using http cloud functions in express without paying anything until a reasonable amount of traffic (after that, is pretty cheap). You could also get rid of cPanel and deploy your frontend there via Firebase hosting.
Maybe you can try to build at first a web application with express. Of course you can create a web app without express if you need it. With express and Node.js I created a MySQL REST API. With HTML and Ajax you can fetch the Data from the API. So you can create two applications. One application where you need to run Node.js because it`s much easier to create a REST API with express. The second one is fully without Node.js.
Maybe there are better solutions, but inside each Web Application you can than but you can then access this API in any web application using jQuery. It doesn't matter if it is written with PHP, ASP.Net Core, Java EE / EE4J. You can also access this API in Ruby, Angular, React, Vue etc. using an AJAX request.
In some scenarios you can't start Node.js as a server because an application is already running on apache2 or nginx. There this would be a workaround to use something like this. For example, one could also integrate applications with HTML+JS in a CMS system that accesses other database tables and thus extend such a system without an iframe.
So can be helpful for few scenarios. Now just doesn't get around the actual goal of doing without Node.js completely or even express. But why are there REST APIs? So that you can query the data and incorporate it somewhere else. Otherwise you would have to build a REST API with another technology. Especially in the example of accessing MySQL with JavaScript, this would not be quickly feasible.
If you are looking for a similar solution to separate the web app and the REST API, but you don't need Node.js, then you should really build a REST API with .Net Core or with another technology, depending on what is possible and installed on your server. It could be Java or PHP behind it or Ruby.
The API that provides the REST access does not have to be written in JavaScript. You only need to be able to access it with JavaScript. So you can use many different approaches to access JSON data. I hope that in the short time with my bad English I have explained the basic idea, how to proceed stylistically and where advantages exist in REST interfaces.
With this, it should be self-explanatory that you don't have to use NodeJS and Express, but with JavaScript it's a pleasant solution. Only you have to ask yourself if a JavaScript application has to provide this interface at all or if in the end only a JavaScript application has to access this interface. Very big difference.
For backend rest api you can use golang with gorilla framework. Golang simple keyword and easy to learn.best important point is performance. If your server support golang you can use golang for backend..
ExpressJS is NodeJS framework so it's impossible to create an API without NodeJS.
Angular is front-end framework so you can host it on web hosting server.
If you need to create back-end APIs, you can use other clouding host servers that support NodeJS.
It's fairly simple to build this with just the net/http package. Set up a router that handles various commands and deal with the response accordingly.

How to serve a decoupled React frontend?

So I am creating a multiplatform application using React & Spring (Java)
I want to make the Spring backend a REST webservice that can be called by both a React-Native and a React frontend.
I currently have my project broken up into 3 sub projects: backend (Spring), webapp-frontend (React webapp), mobile-frontend (React native)
My question is on how I should actually serve the React webapp frontend. I will have the webservice on a server somewhere so that the React code can hit it to make API calls, but as far as serving the React webapp would it be better to do serve it with the same backend server or would it be better to make a seperate frontend server with something like express? Also, are there any other alternatives?
Serving the react webapp with either express or the same server as the REST backend is both valid options. I would say that if there is no specific reason for choosing express, serving it on the same server as the spring backend is your best choice.
Recall that the frontend and backend will still be decoupled and only communicate through the REST-interface so to extend the backend-server to host the react-webapp should be quite simple, here's an example that could be helful: React,Spring,Rest example
However there is a couple arguments that I see that could make you choose a separate server then the REST backend, for example:
Isolated restarts and failures. If you want to be able to restart only your react-frontend without affecting the REST backend you will probably want to run them on different servers. Another thing to consider is if you expect that the backend will need to scale heavily and you'll need to replicate the backend across multiple servers, that might be easier to do if the frontend is running on its own separate server.
Server-side rendering of JavaScript (React) is possible through a node.js server like express but I don't think it is possible with a Spring server.
Also, are there any other alternatives?
Other alternatives then express/Spring, Yes. Other alternatives then hosting the fronted on the backend-server or on its own server? No
There are pros and cons with both choices and what is best depends on your needs.

Advice on building a server connected android app

I'm creating a personal project, an Android application, where user sign ins via a server are essentially. Also users will be able to share important "updates" in both a data stream that all users can have access to, and potentially down the line a data stream that will be specific to local users.
Unfortunately I have basically no idea of how to implement the server for this application. I'm confident that I'll be able to create a solution though, I just need to be pointed in the right direction. Are there any existing servers/api's that I could access that will allow me to handle my "connected" tasks? If I were to make a server myself for this application where should I start?
If you have absolutely no experience with server side coding, you should start with PHP and MySQL to create a simple API for your app.
For getting started download the XAMPP bundle that has everything you need.
SQL and PHP are fairly easy for beginners. On the client(Android) side use Volley as it is simple to understand.
However if you want to get started with Node.js, your server can be up in a few minutes with express.js

Hiding Parse javascript SDK files and blocking client from running js

I'm working on a Parse web app and have run into some problems using the backbone.js based client side javascript sdk. I noticed the way I have things set up, the client can view all of my source code by simply using the dev tools to view source files and can also run code against the database (within the limits of the ACL's I've set). I've started working on rebuilding the app in cloud code using the Express.js module Parse provides so that all of my code is stored server side, but I was wondering how those using client side frameworks get around this obvious problem.
That's the issue with client-side code. Assume any code you send to the client is hacked, broken, and tampered with.
With JavaScript, your best bet is to use either Cloud Code and send AJAX or streaming data calls to the server, retrieve the data from the server at runtime (not super secure, but would fool some people), or accept that your code is vulnerable.
I typically work with frameworks in the MVC format, so I only expose a limited subset of the actual model via a REST API. I use both a client-side framework and a server-side framework. Any thing sensitive goes on the server.

Run javascript on rails server side

I have a rails app which is the REST api. Part of the models i have are javascript scripts that the users are able to upload.
I want to create a background worker task where i can make a network request and execute the javascript on the result of the network request server side and then save it to the database.
Now i have been told to consider using a Rails front end api and then use a NodeJS server for being the workers and executing javascript.
My main concern is developing a cross platform schema with nodejs orm and rails orm.
So my question is twofold is there a way of developing the database schema so it is in sync across both the Node JS server and the Rails server. Else is there a way of executing the javascript on the rails server?
You can use https://github.com/balderdashy/waterline or https://github.com/sequelize/sequelize or https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Shell to access same database as Rails app from Node.js app but make sure that you are only reading from database using Node.js not writing

Categories

Resources