Portable Javascript Client for a REST API - javascript

I'm about to create a JavaScript-based client for a RESTful webservice. The client should facilitate access to the webservice and wrap some raw HTTP calls into more "candy" functions and objects.
I'm intending to use this client library for node.js-based applications as well as for PhoneGap. So, I don't have to care about the Same-origin-policy. However, dispatching HTTP requests is totally differnt in PhoneGap and node.js.
Now I'm wondering how I can implement a client library in JavaScript, that is portable between different "platforms" (node.js, PhoneGap, perhaps later even browers)?
Thanks in advance

You might try this emulation of XmlHttpRequest under nodejs but you still may need to special-case your client library to operate under PhoneGap (and other browser-based JavaScript platorms) and nodejs.
One way to do this would be to check the contents of 'window' which is undefined by default under nodejs, or 'global' which is (should be) undefined in a browser.
EDIT
It appears I spoke(wrote) too soon. Check out abstract-http-request which while not explicitly supporting PhoneGap, might give you enough to work with.

Phonegap is basically just a browser. So if you want to get around the same origin policy you might want to take a look into different phonegap plugins (GapSocket) to handle your own communication.

REST is an architectural style for designing web services. A web service doesn't have to use HTTP to be RESTful, though a large proportion of them do. To be RESTful, an HTTP-based web service has to rigorously leverage HTTP's resource names (URIs), operations (GET, PUT, DELETE, etc), error codes (404, 200, ...), and so on. This means that any (HTTP-based) REST client framework can interoperate with any (HTTP-based) REST server framework: if it doesn't, something's probably wrong. So your problem decomposes into finding a good generic REST client framework and one or more generic REST server frameworks.
For node.js based web services, take a look at the Geddy server-side framework (here's the documentation).
For a PhoneGap REST client, you could try the Force.com JavaScript REST Toolkit (and see this extension).
Disclaimer: I have only read about these frameworks, not used them.

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.

Server side for dojo-based+Qt5 application

I'm studing dojo to create web applications.
The HTTP server will be a Windows machine with IIS.
On the same machine will run my own Qt5 application to provide all backend logic for the web pages. I'm going to use dojo for them.
I wonder what is the best way to exchange data (JSON) between the client (dojo) and my application (Qt5).
In the docs I've learned how to use the AJAX methods to make HTTP requests (https://dojotoolkit.org/documentation/tutorials/1.10/ajax/index.html). But they rely on the HTTP server, instead I need to communicate with my Qt5 application.
What do you recommend?
Of course I'm trying to avoid (web)sockets!
The Dojo main modules for loading data in your application are based on AJAX, examples require, dojo.xhrGet, the content is actually in JSON format. Dojo has also support for restful service with module as dojo/store/JsonRest.
Dojo also offer you some support for socket using dojox
It really depends of the scope of your Qt5 application if you need bidirectional communication web sockets could be a reasonable solution.

Is Node.Js compatible with iPhone Objective C development for apps?

I am a designer interested in making a shift to iPhone App Development. I am looking to spend the weeks after my exams studying how backend and frontend iPhone development works.
If I want to ultimately build an app which requires a frontend work in ObjectiveC/Xcode environment will I be able to use Node.js for example to compile user data and databases for backend data?
As I understand it (please correct me if I am wrong) to do an app which connects to servers for data requests you need a backend development. I have been reading about node.js and it seems very fast and its javascript which I like.
What would be the easiest combo to get into. I really am not technical and want to limit the pain for compatibility issues.
Will Objective C and Node.js be compatible?
Do you have any outside recommendations with experience you like to share?
Thank you
At a high level, any web server that has the ability to accept http requests and respond with some content (JSON, XML, HTML, string...) will work, you just have to use the correct methods for submitting the request and parsing the response.
Personally, I've been using node.js for an API that I created and host it on AWS. It's lightning fast and I've had no issues. As with most programming languages today, objective-c has libraries that allow you to submit http requests and parse JSON responses.
Node.js supply web interface just like any other backend stack you can choose, so it is suitable for iPhone backend development.
You can also skip the entire backend development, hosting solution and DNS boilerplate if you wish, by using backend as a service solutions like Parse or Stackmob

REST client utility

I'm developing a REST API, and am looking for a simple graphic utility that will let me test the full set of REST verbs (GET, PUT, POST, and DELETE); GET is obviously not a problem, but I'd like something that can encapsulate the basic functionality of being able to specify a content body for PUT / POST, specify headers, etc. I'm sure someone's done this sort of thing before, but I can't seem to find any references to a simple graphical bit of HTML / Javascript that will allow me to specify an endpoint, headers, body, etc. and execute a REST request. Such a thing would be very useful in many environments. Does anyone know of such a tool that's available without any licensing issues?
Note: ideally, I'd like to find something that can be embedded in a web page, i.e. no browser add-ons.
Although you said that browser plugins are second choice for you, I am using Postman Chrome plugin happily and recommend it.
I know this is not something you can embed, but if you just want to test your REST service with the relevant HTTP verbs I would recommend using Fiddler.
I once had similar requirement (test-utility inlined inside online-documentation to provide sample calls). Also no luck and couldn't find anything.
In the end I did:
Provide link to browser-addon RESTClient
Building little REST spefic HTML form (radio-buttons for verbs, URL field + payload). Backend was a simple controller respectively which was then forwarding the parameters to the real api. Very important was syntax highligthing for payload (XML, JSON) for me. For that I used nice JS-library code-mirror. Further more I had more control on the form (e.g. hiding/showing certain HTTP verb after entering URL). Overall building simple form as Rest Client was much less effort than I thought, which again shows that a good Restful API over HTTP makes clients very easy to develop.
Try Okapi: two files you can just drop into your webserver. It's free and free.
WizTools.org RESTClient
RESTClient is an excellent open source Java application you may want to try that can be used to test a variety of HTTP communications (it supports GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE). It features a GUI version and a CLI version to suit either manual or automated testing. Note that it also provides SSL and scripting support.
Though it can't be embedded in a web page (as per your request), being a Java application, it can easily be run on numerous plaforms. As it is my own REST API testing tool of choice, I can highly recommend it.
Screenshot:
I've included this last as your "ideal" said no browser add-ons, however ... I've tried numerous REST clients and by far the best I've used is the Chrome app: DHC.
DHC (aka Dev HTTP Client) is designed and developed by a developer for developers to make direct HTTP resource discovery, manipulation and testing more easily. Beside the main function, sending/receiving custom HTTP requests/responses, it allows permanently to save a request to a local repository for later reuse and moreover the request declaration can include variables that are context specific. With the use of contexts you can easily switch between various environments without modifying request declaration. (e.g. from a test environment to production)
If you can get past the browser add-on aspect, I would strongly recommend giving DHC a shot.

PostgreSQL sockets from JavaScript (HTML5)

I'm looking at options to connect directly--without a web server or middleware--to a PostgreSQL server using JavaScript from a web browser client. On github, I found three projects:
node_postgres
node-postgres
postgres-js
They all appear to be in early but at least somewhat active development.
Do they all do roughly the same thing? Is what they do even what I'm looking for? Does anyone have experience with any of them that could recommend one over the others?
node-postgres was inspired by postgres-js and does roughly the same thing.
However, they both seem to be their own sort of middleware, because they require node.js, which is a server-side JavaScript implementation of a web server. So they would cut out a layer, but still not be the same thing as connecting directly to the PostgreSQL server.
There might be a way to combine the code in them with some HTML5 socket examples, though, to make connections directly from a web browser client.
If you are interested in CLIENT side JavaScript, as the OP's question implied, but you don't insist on owning the server, there is a commercial service that can help you.
The Rdbhost service makes PostgreSQL servers accessible from client-side JavaScript. There is a security system to prevent unauthorized queries, using a server-side white-list and an automated white-list populating system.
It uses plain old AJAX style http requests, provides a jQuery extension to facilitate the querying.
See https://www.rdbhost.com .
There is no secure solution today. One of possible solutions would be htsql:
http://htsql.org/
However there you use web addresses to query, even with https your queries will be plain text!
You should/could use a small webserver to handle requests. Alternativelly you can write an app, or use a local postgres server to handle the connection (in this case you still will need some kind of webserver).
The problem is very simple: your webbrowsers are limited in protocols to talk to the web, and postgres is not on this list. In fact you should not try to overcome this issue, using a server-client architecture is a very good solution. Format your request with JS to make it as small as possible, and let your web-server scripts interpret it into functional sql requests. The answer can be parsed into shorter response, then a sql data transfer, and you just need to interpret it on your side. Since you will create interperers on all sides, you will achieve a higher abstraction then in case of direct db connection, and thus independency towards the backend engines you use.

Categories

Resources