Giter Site home page Giter Site logo

electron-middle's Introduction

electron-middle

electron-middle is a way to add middleware behaviour to electron apps

To install, run npm install electron-middle

Example usage

This example will open a window that displays "Hello, world!"

// index.js
// Run this file with electron:
//   electron index.js
const middle = require('electron-middle')

// This is where the magic happens
middle.get((file, cb) => {
  if (file === "/index.html") {
    // Calling cb with a string or a buffer will behave as if a file was
    // found with that string or buffer as its contents
    cb("<p>Hello, world!</p>")
  } else {
    // Calling cb with no parameters will pass the buck to the next
    // middleware, and if there is none, the default file fetcher will
    // simply try to load a local file
    cb()
  }
})


function createWindow () {
  let win = new BrowserWindow({width: 800, height: 600})
  
  win.loadURL(url.format({
    pathname: 'index.html',
    protocol: 'file:',
    slashes: true
  }))
}

Usage

middle.get() takes a function with two arguments, file and cb.
file is the path to the file that electron is trying to access. (As you can see in the example above, the file doesn't need to actually exist).
cb is a function you must call exactly once, call it with a string or a Buffer if successful, or empty to let the next, or default handler deal with the request.

Example implementation

Here is the main code for electron-middle-pug, a middleware to compile .pug files to .html

// electron-middle-pug: index.js
const middle = require('electron-middle')
const pug = require('pug')
const fs = require('fs')
const path = require('path')

middle.get((file, cb) => {
  let ext = path.extname(file);
  if (ext == '.html') {
    let pugfile = file.replace('.html', '.pug')
    fs.access(pugfile, err => {
      if (err) {
        cb()
      } else {
        compilePug(pugfile, cb)
      }
    })
  } else {
    cb()
  }
})

function compilePug(pugfile, cb) {
  fs.readFile(pugfile, (err, data) => {
    if (err) {
      cb()
    } else {
      let fn = pug.compile(data.toString(), {})
      let html = fn({})
      cb(html)
    }
  })
}

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.