koa-mount-express

1.0.1 • Public • Published

koa-mount-express

Mount Express applications within a Koa application as middleware. The path passed to mountExpress() is removed from the ctx.req.url before directing ctx.req and ctx.res to the Express app. This is useful for mounting an external Express app within your own Koa app.

Installation

npm install koa-mount-express

Usage

import express from "express";
import Koa from "koa";
import mountExpress from "koa-mount-express";

const app = new Koa();
const expressApp = express();

app.use(mountExpress("/express", expressApp));

app.listen(3000);

Example

import express from "express";
import Koa from "koa";
import mountExpress from "koa-mount-express";

const app = new Koa();
const expressApp = express();

expressApp.get("/", function (req, res) {
  res.send("Hello Express");
});

expressApp.get("/route", function (req, res) {
  res.send("Hello Express toute");
});

// this will mount express app on /express route
// Note: this should be before other koa middleware
app.use(mountExpress("/express", expressApp));

app.use((ctx) => {
  ctx.body = "Hello Koa";
});

app.listen(3000);

Example responses:

GET /
Hello Koa

GET /express
Hello Express

GET /route
Hello Express route

GET /express/not-a-route
Cannot GET /not-a-route

Package Sidebar

Install

npm i koa-mount-express

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

5.44 kB

Total Files

6

Last publish

Collaborators

  • jackbuehner