Static file serving for Electron apps
Switch branches/tags
Nothing to show
Clone or download
Latest commit 85b8813 Jun 8, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Replace pify with util.promisify (#5) May 22, 2018
.editorconfig Init Jan 11, 2018
.gitattributes Init Jan 11, 2018
.gitignore Init Jan 11, 2018
.npmrc Init Jan 11, 2018
.travis.yml Init Jan 11, 2018
index.js Add `partition` option (#4) Jun 8, 2018
license Init Jan 11, 2018
package.json 0.2.0 Jun 8, 2018
readme.md Add `partition` option (#4) Jun 8, 2018

readme.md

electron-serve Build Status

Static file serving for Electron apps

Normally you would just use win.loadURL('file://…'), but that doesn't work when you're making a single-page web app, which most Electron apps are today, as history.pushState()'ed URLs don't exist on disk. It serves files if they exist, and falls back to index.html if not, which means you can use router modules like react-router, vue-router, etc.

Install

$ npm install electron-serve

Usage

const {app, BrowserWindow} = require('electron');
const serve = require('electron-serve');

const loadURL = serve({directory: 'renderer'});

app.on('ready', () => {
	mainWindow = new BrowserWindow();

	loadURL(mainWindow);

	// The above is equivalent to this:
	mainWindow.loadURL('app://-');
	// The `-` is just the required hostname
});

API

serve(options)

directory

Required
Type: string

The directory to serve, relative to the app root directory.

scheme

Type: string
Default: app

Custom scheme. For example, foo results in your directory being available at foo://-.

partition

Type: string
Default: electron.session.defaultSession

The partition the protocol should be installed to, if you're not using Electron's default partition.

Related

License

MIT © Sindre Sorhus