Skip to content
Express like API for hosting content inside an Electron app
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
example Add documentation, readme and clean up example Nov 6, 2016
src Allow setting of mimeType May 10, 2017


Build Status npm version npm license status

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) => {
    hello: 'world',

// Renderer Process
import { rendererPreload } from 'electron-routes';


  .then(resp => resp.json())
  .then(o => console.log(o)); // { hello: 'world' }


Router([schemeName = 'app', partitionName])

router.[method](pathPattern, handler)

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 - A Request object
  • response - A Response object
  • next - A function to tell electron-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.

router.use(pathPattern, handler)

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.

response.send(string[, mimeType='text/html'])

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


Sindre Sorhus
Samuel Attard


MIT © Sindre Sorhus

You can’t perform that action at this time.