Use an Express style API that you know and love without the security concerns and hassle of making it work inside Electron.
$ npm install --save electron-routes
// Main Process
import { Router } from "electron-routes";
const api = new Router("myscheme");
api.get("foo", (req, res) => {
res.json({
hello: "world",
});
});
// Renderer Process
import { rendererPreload } from "electron-routes";
rendererPreload();
fetch("myscheme://foo")
.then((resp) => resp.json())
.then((o) => console.log(o)); // { hello: 'world' }
schemePrivileges
is the same structure as found in the Electron docs.
NOTE: method can be any standard HTTP method (get
, post
, put
, delete
)
This will set the handler function to be called whenever a path is requested that
mathces the provided pathPattern
and the request method is the same as method
;
The handler
function will be called with:
request
- ARequest
objectresponse
- AResponse
objectnext
- A function to tellelectron-router
to attempt the next handler
Note, if you call next
in all the handlers electron-router
will automatically
send a 404 File Not Found error message.
If handler
is a function this will act in exactly the same way as the generic
method
function above. However, if the handler
is a MiniRouter
it will pass
all requests that match the pathPattern
through to the MiniRouter
, but only
after all standard method handlers have been called on the current router.
All handlers on the current router will take precedence over handlers on a sub-router.
MiniRouter has an identical API to the main Router
class. Just make
sure you only use the main Router
class for the top level router though, or bad
things will happen.
This method will enable the registered schemes to work with the fetch
API and
bypasses certain CORS
errors that can occur. 99.99% of the time you will need
to call this method.
NOTE: It's probably a good idea to call it in a preload script
An object of all the URL params that resulted in this path being valid. E.g. The
path foo/:thing/hello
would match if you requsted myscheme://foo/magical/hello
.
In this case request.params
would be equal to:
request.params = {
thing: "magical",
};
The HTTP method that caused this request. Normally one of get
, post
, put
or delete
.
An array of Electron's uploadData
objects. They follow the same structure as
found in the Electron docs
but with two extra methods.
Automatically attempts to convert the bytes
Buffer of the uploadData into a string
and return it.
Automatically attempts to convert the bytes
Buffer of the uploadData into a
javascript object and return it.
Will immediately terminate the request sending a stringified version of the object back to the client.
Will immediately terminate the request sending the string as the response text back to the client.
Will immediately terminate the request with a 404 File Not Found response
MIT ©