New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

electron-serve

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-serve

Static file serving for Electron apps

  • 2.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

electron-serve

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

Requires Electron 30 or later.

Usage

import {app, BrowserWindow} from 'electron';
import serve from 'electron-serve';

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

let mainWindow;

(async () => {
	await app.whenReady();

	mainWindow = new BrowserWindow();

	await loadURL(mainWindow);

	// Or optionally with search parameters.
	await loadURL(mainWindow, {id: 4, foo: 'bar'});

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

API

loadUrl = serve(options)

options

Type: object

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://-.

hostname

Type: string
Default: '-'

Custom hostname.

file

Type: string
Default: 'index'

Custom HTML filename. This gets appended with '.html'.

isCorsEnabled

Type: boolean
Default: true

Whether CORS should be enabled. Useful for testing purposes.

partition

Type: string
Default: electron.session.defaultSession

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

loadUrl(window, searchParameters?)

The serve function returns a loadUrl function, which you use to serve your HTML file in that window.

window

Required
Type: BrowserWindow

The window to load the file in.

searchParameters

Type: object | URLSearchParams

Key value pairs or an URLSearchParams instance to set as the search parameters.

Keywords

FAQs

Package last updated on 17 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc