Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions azure-functions-nodejs-extensions-express/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# @azure/functions-extensions-express

Express integration for Azure Functions - run Express apps natively within Azure Functions with full middleware and streaming support.

## Features

- **Native Express** - Express runs as a real HTTP server, not an adapter
- **Full Middleware Support** - All Express middleware works without modification
- **Streaming** - Server-Sent Events and large responses stream natively
- **Zero Code Changes** - Use your existing Express app as-is

## Installation

```bash
npm install @azure/functions-extensions-express express
```

## Quick Start

```typescript
import express from 'express';
import { expressApp } from '@azure/functions-extensions-express';

// Create your Express app
const app = express();

app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});

app.get('/users', (req, res) => {
res.json([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]);
});

// Register with Azure Functions
expressApp('api', app, {
route: 'api/{*path}',
basePath: '/api'
});
```

## Streaming Example (Server-Sent Events)

```typescript
import express from 'express';
import { expressApp } from '@azure/functions-extensions-express';

const app = express();

app.get('/stream', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');

let count = 0;
const interval = setInterval(() => {
res.write(`data: ${JSON.stringify({ event: ++count })}\n\n`);
if (count >= 10) {
clearInterval(interval);
res.end();
}
}, 1000);
});

expressApp('api', app, {
route: 'api/{*path}',
basePath: '/api',
enableStreaming: true
});
```

## API Reference

### `expressApp(name, app, options?)`

Registers an Express application as an Azure Function.

**Parameters:**
- `name` - Function name (string)
- `app` - Express application
- `options` - Configuration options (optional)

**Options:**
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `route` | string | `'{*path}'` | Azure Functions route pattern |
| `basePath` | string | `''` | Path prefix to strip before routing to Express |
| `methods` | string[] | All methods | HTTP methods to accept |
| `authLevel` | string | `'anonymous'` | Azure Functions auth level |
| `enableStreaming` | boolean | `true` | Enable streaming responses |
| `timeout` | number | `30000` | Request timeout (ms) |
| `port` | number | `0` | Express server port (0 = auto) |

**Returns:** `ExpressServer` - Server instance for lifecycle management

### `stopAllServers()`

Stops all Express servers. Use during app termination.

```typescript
import { app } from '@azure/functions';
import { stopAllServers } from '@azure/functions-extensions-express';

app.hook.appTerminate(async () => {
await stopAllServers();
});
```

### `getServer(name)`

Gets an Express server by function name.

```typescript
import { getServer } from '@azure/functions-extensions-express';

const server = getServer('api');
console.log(`Server running on port ${server?.port}`);
```

## How It Works

This extension runs Express as a real HTTP server on localhost within the Azure Functions worker process. When an HTTP request arrives at Azure Functions:

1. The request is proxied to the Express server
2. Express handles it with full middleware support
3. The response is streamed back to the client

This architecture provides true Express compatibility without adaptation layers.

## Requirements

- Node.js 18+
- @azure/functions ^4.0.0
- Express ^4.0.0 or ^5.0.0

## License

MIT
83 changes: 83 additions & 0 deletions azure-functions-nodejs-extensions-express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions azure-functions-nodejs-extensions-express/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@azure/functions-extensions-express",
"version": "0.1.1-preview",
"description": "Express integration for Azure Functions - run Express apps natively in Azure Functions",
"main": "dist/index.js",
"types": "types/index.d.ts",
"license": "MIT",
"author": "Microsoft Corporation",
"homepage": "https://github.com/Azure/azure-functions-nodejs-extensions-express#readme",
"repository": {
"type": "git",
"url": "https://github.com/Azure/azure-functions-nodejs-extensions.git",
"directory": "/azure-functions-nodejs-extensions-express"
},
"keywords": [
"azure",
"functions",
"serverless",
"express",
"nodejs"
],
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"lint": "eslint src/**/*.ts",
"test": "mocha --require ts-node/register test/**/*.test.ts"
},
"engines": {
"node": ">=18.0"
},
"dependencies": {
"@azure/functions": "^4.11.0",
"@types/node": "^18.0.0",
"typescript": "^5.0.0"
},
"peerDependencies": {
"@azure/functions": "^4.11.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"ms-azuretools.vscode-azurefunctions"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Node Functions",
"type": "node",
"request": "attach",
"restart": true,
"port": 9229,
"preLaunchTask": "func: host start"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.postDeployTask": "npm install (functions)",
"azureFunctions.projectLanguage": "TypeScript",
"azureFunctions.projectRuntime": "~4",
"debug.internalConsoleOptions": "neverOpen",
"azureFunctions.projectLanguageModel": 4,
"azureFunctions.preDeployTask": "npm prune (functions)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"label": "func: host start",
"command": "host start",
"problemMatcher": "$func-node-watch",
"isBackground": true,
"dependsOn": "npm watch (functions)"
},
{
"type": "shell",
"label": "npm build (functions)",
"command": "npm run build",
"dependsOn": "npm install (functions)",
"problemMatcher": "$tsc"
},
{
"type": "shell",
"label": "npm watch (functions)",
"command": "npm run watch",
"dependsOn": "npm install (functions)",
"problemMatcher": "$tsc-watch",
"group": {
"kind": "build",
"isDefault": true
},
"isBackground": true
},
{
"type": "shell",
"label": "npm install (functions)",
"command": "npm install"
},
{
"type": "shell",
"label": "npm prune (functions)",
"command": "npm prune --production",
"dependsOn": "npm build (functions)",
"problemMatcher": []
}
]
}
Loading
Loading