1.0.1 • Published 3 years ago

@microbackend/plugin-express v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 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

3 years ago

1.0.1

3 years ago

1.0.1-alpha.27

3 years ago

1.0.1-alpha.31

3 years ago

1.0.1-alpha.43

3 years ago

1.0.1-alpha.42

3 years ago

1.0.1-alpha.41

3 years ago

1.0.1-alpha.15

3 years ago

1.0.1-alpha.0

4 years ago

1.0.0

4 years ago

1.0.1-alpha.135

4 years ago

1.0.1-alpha.133

4 years ago

1.0.1-alpha.131

4 years ago

1.0.1-alpha.129

4 years ago

1.0.1-alpha.126

4 years ago

1.0.1-alpha.124

4 years ago

1.0.1-alpha.123

4 years ago

1.0.1-alpha.122

4 years ago

1.0.1-alpha.120

4 years ago

1.0.1-alpha.113

4 years ago

1.0.1-alpha.108

4 years ago

1.0.1-alpha.107

4 years ago

1.0.1-alpha.101

4 years ago

1.0.1-alpha.99

4 years ago

1.0.1-alpha.97

4 years ago

1.0.1-alpha.96

4 years ago

1.0.1-alpha.94

4 years ago

1.0.1-alpha.91

4 years ago

1.0.1-alpha.86

4 years ago

1.0.1-alpha.85

4 years ago

1.0.1-alpha.82

4 years ago

1.0.1-alpha.81

4 years ago

1.0.1-alpha.79

4 years ago

1.0.1-alpha.75

4 years ago

1.0.1-alpha.70

4 years ago

1.0.1-alpha.69

4 years ago

1.0.1-alpha.68

4 years ago

1.0.1-alpha.65

4 years ago

1.0.1-alpha.62

4 years ago

1.0.1-alpha.61

4 years ago

1.0.1-alpha.58

4 years ago

1.0.1-alpha.56

4 years ago

1.0.1-alpha.53

4 years ago

1.0.1-alpha.52

4 years ago

1.0.1-alpha.49

4 years ago

1.0.1-alpha.48

4 years ago

1.0.1-alpha.47

4 years ago

1.0.1-alpha.45

4 years ago

1.0.1-alpha.40

4 years ago

1.0.1-alpha.39

4 years ago