1.0.1 • Published 2 years ago

@microbackend/plugin-express v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@microbackend/plugin-express

Microbackend plugin for express.js.

Installation

This plugin is automatically included if you had initialized a microbackend project with:

npx microbackend init --template express

To install it as a standalone dependency:

npx microbackend plugin add @microbackend/plugin-express

Usage

For illustration purposes, create the following files in your project:

  • src/extension/express/route/route1.ts:
import { IMicrobackendInitFunction } from "@microbackend/plugin-express";

export default ((): IMicrobackendRouteCreator => {
  return () => {
    return {
      handler: async (_req, res) => {
        res.json({ value: 1 });
      },
      method: "get",
      route: "/route1",
    };
  };
})();
  • src/extension/express/route/middleware1.ts:
import { IMicrobackendMiddlewareCreator } from "@microbackend/plugin-express";

export default ((): IMicrobackendMiddlewareCreator => {
  return () => {
    return {
      handler: (req, _res, next) => {
        console.log(req);
        next();
      },
    };
  };
})();

Then, in your main application entry file:

import { createExpressApplication } from "@microbackend/plugin-express";
import express from "express";

export default createExpressApplication().then((app) => {
  app.use(((err, _req, res, _next) => {
    res.status(err.status || 500).json({ error: err.message });
  }) as express.ErrorRequestHandler);

  const port = process.env["PORT"] || 3001;

  app.listen(port, () => {
    console.log("Started example server on port", port);
  });
});

Now, your Express app will automatically have a middleware that logs all requests, and a request handler registered for route /route1.

In short, the extension patterns supported by this plugin are:

  • IMicrobackendRouteCreator, supported by extension/express/route: A function type that creates an Express route. The handler option can either be a function or an express.Router instance.

  • IMicrobackendMiddlewareCreator, supported by extension/express/middleware: A function type that creates an Express middleware.

For more information on extension patterns, please have a look at @microbackend/plugin-core.

1.0.1-alpha.26

2 years ago

1.0.1

2 years ago

1.0.1-alpha.27

2 years ago

1.0.1-alpha.31

2 years ago

1.0.1-alpha.43

2 years ago

1.0.1-alpha.42

2 years ago

1.0.1-alpha.41

2 years ago

1.0.1-alpha.15

2 years ago

1.0.1-alpha.0

2 years ago

1.0.0

2 years ago

1.0.1-alpha.135

2 years ago

1.0.1-alpha.133

2 years ago

1.0.1-alpha.131

2 years ago

1.0.1-alpha.129

2 years ago

1.0.1-alpha.126

2 years ago

1.0.1-alpha.124

2 years ago

1.0.1-alpha.123

2 years ago

1.0.1-alpha.122

2 years ago

1.0.1-alpha.120

2 years ago

1.0.1-alpha.113

2 years ago

1.0.1-alpha.108

2 years ago

1.0.1-alpha.107

2 years ago

1.0.1-alpha.101

2 years ago

1.0.1-alpha.99

2 years ago

1.0.1-alpha.97

2 years ago

1.0.1-alpha.96

2 years ago

1.0.1-alpha.94

2 years ago

1.0.1-alpha.91

2 years ago

1.0.1-alpha.86

2 years ago

1.0.1-alpha.85

2 years ago

1.0.1-alpha.82

2 years ago

1.0.1-alpha.81

2 years ago

1.0.1-alpha.79

2 years ago

1.0.1-alpha.75

2 years ago

1.0.1-alpha.70

2 years ago

1.0.1-alpha.69

2 years ago

1.0.1-alpha.68

2 years ago

1.0.1-alpha.65

2 years ago

1.0.1-alpha.62

2 years ago

1.0.1-alpha.61

2 years ago

1.0.1-alpha.58

2 years ago

1.0.1-alpha.56

2 years ago

1.0.1-alpha.53

2 years ago

1.0.1-alpha.52

2 years ago

1.0.1-alpha.49

2 years ago

1.0.1-alpha.48

2 years ago

1.0.1-alpha.47

2 years ago

1.0.1-alpha.45

2 years ago

1.0.1-alpha.40

2 years ago

1.0.1-alpha.39

2 years ago