https://github.com/jackbuehner/koa-mount-express
https://github.com/jackbuehner/koa-mount-express
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jackbuehner/koa-mount-express
- Owner: jackbuehner
- License: mit
- Created: 2024-09-25T16:50:56.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-09-25T16:58:07.000Z (8 months ago)
- Last Synced: 2025-03-26T10:48:59.912Z (about 2 months ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 KoaGET /express
Hello ExpressGET /route
Hello Express routeGET /express/not-a-route
Cannot GET /not-a-route
```