An open API service indexing awesome lists of open source software.

https://github.com/jackbuehner/koa-mount-express


https://github.com/jackbuehner/koa-mount-express

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# 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

```bash
npm install koa-mount-express
```

## Usage

```js
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

```js
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
```