formio / sw-express

Extend express so it will work in service workers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Express Service Worker

Extend express to run as a service worker in the browser

Install

npm install sw-express

Use

import { express } from 'sw-express';

const app = express();

// This is a custom body parser for fetch requests.
app.use((req, res, next) => {
  req.request.text().then(text => {
    if (text) {
      // Attempt to convert to an object if it is json. Otherwise return the text.
      try {
        req.body = JSON.parse(text);
      }
      catch (err) {
        req.body = text;
      }
    }
    next();
  });
});

app.use((req, res, next) => {
  // Write your own middleware as usual.
  next();
});

// Handle errors
app.use((err, req, res, next) => {
  if (err) {
    console.error(err);
  }
  next();
});

// Listen to a URL, not a port. This will intercept any requests made to this url.
app.listen('http://myserver.com');

About

Extend express so it will work in service workers


Languages

Language:JavaScript 55.0%Language:TypeScript 45.0%