REST API in electron

13,734

When implementing a RESTful service for an app, you would essentially place your MongoDB and your server-side script, the one that handles the REST calls, on an accessible server, if you have it running, the server in your case is probably your machine.

You would then use HTTP GET, POST, PUT, DELETE and whatever functionality you built into your service via a client, in your case an Electron app.

To explain a bit better, your API lives in one place and you access/manipulate that data via HTTP calls, in layman's using a url, from anywhere with access to that domain, such as internet connected mobiles and computers or other websites.

Whatever language you develop the client in, look into the native or third party HTTP libraries to access and grab that data.

Simple JS Client Call Example:

function httpGet(theUrl) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

You would then handle the data with the proper parser, I'm assuming you are targeting JSON. From there you can assign and use that data.

If you are doing it locally first, for your url use the localhost and the port MongoDB is listening on.

If you want to deploy and test live, I recommend Amazon Web Services Elastic Beanstalk or EC2. AWS is free for a year and also has a great free tier perfect for prototyping and home projects. I am utilizing one for a rest service now!

Share:
13,734
Shriharsha KL
Author by

Shriharsha KL

I work for Clumio, a cloud-based backup as a service for Enterprise. I am one of the early engineers who helped design a scalable front-end infrastructure using modern open-source technologies like React, Typescript and Webpack.

Updated on June 06, 2022

Comments

  • Shriharsha KL
    Shriharsha KL almost 2 years

    Where and how do I write a REST API in an electron app? I have already written the API (which accesses MongoDB to add/remove/modify objects in the DB) and it works well in a NodeJS app. But I am unclear on how to do the same in an electron app.

  • Joe Leonard
    Joe Leonard over 6 years
    I find that information on this topic is very sparse. Does this actually work using electron? Don't you have to enable CORS in order to do this?