Coder Social home page Coder Social logo

electron-serve's Introduction

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 8 or later.

Usage

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

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

let mainWindow;

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

	mainWindow = new BrowserWindow();

	await loadURL(mainWindow);

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

API

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

isCorsEnabled

Type: boolean
Default: true

Whether CORS should be enabled. Useful for testing purposes.

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

electron-serve's People

Contributors

sindresorhus avatar hadeeb avatar dedo1911 avatar akshitkrnagpal avatar ffflorian avatar ibash avatar juliangruber avatar laztoum avatar richienb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.