azure-function-express
Version:
Allows Express usage with Azure Function
117 lines (84 loc) • 3.71 kB
Markdown
<a href="https://hapticmedia.fr/en/">
<img align="right" alt="Supported by Hapticmedia" src="docs/media/hapticmedia-support.svg" title="Supported by Hapticmedia" height="60"/>
</a>
<a href="https://azure.microsoft.com/en-us/services/functions/">
<img align="right" alt="Function logo" src="docs/media/function.png" title="Function" width="150"/>
</a>
> Allows Express usage with Azure Function
[](https://www.npmjs.com/package/azure-function-express)
[](https://github.com/Azure/azure-webjobs-sdk-script/issues/2036#issuecomment-336942961)


[](https://travis-ci.org/yvele/azure-function-express)
[](https://codecov.io/github/yvele/azure-function-express)
[](LICENSE)
[](https://github.com/senchalabs/connect) your [Express](https://expressjs.com) application to an [Azure Function handler](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node), and make seamless usage of [all middlewares](http://expressjs.com/en/guide/using-middleware.html) you are already familiar with.
In your `index.js`:
```js
const createHandler = require("azure-function-express").createHandler;
const express = require("express");
// Create express app as usual
const app = express();
app.get("/api/:foo/:bar", (req, res) => {
res.json({
foo : req.params.foo,
bar : req.params.bar
});
});
// Binds the express app to an Azure Function handler
module.exports = createHandler(app);
```
Make sure you are binding `req` and `res` in your `function.json`:
```json
{
"bindings": [{
"authLevel" : "anonymous",
"type" : "httpTrigger",
"direction" : "in",
"name" : "req",
"route" : "foo/{bar}/{id}"
}, {
"type" : "http",
"direction" : "out",
"name" : "res"
}]
}
```
To allow Express handles all HTTP routes itself you may set a glob star route in a single root `function.json`:
```json
{
"bindings": [{
"authLevel" : "anonymous",
"type" : "httpTrigger",
"direction" : "in",
"name" : "req",
"route" : "{*segments}"
}, {
"type" : "http",
"direction" : "out",
"name" : "res"
}]
}
```
Note that `segments` is not used and could be anything. See [Azure Function documentation](https://github.com/Azure/azure-webjobs-sdk-script/wiki/Http-Functions).
All examples [here](/examples/).
All native Azure Functions [context](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#context-object) properties, except `done`, are exposed through `req.context`.
As en example, you can [log](https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#writing-trace-output-to-the-console) using:
```js
app.get("/api/hello-world", (req, res) => {
req.context.log({ hello: "world" });
...
});
```
Supported Node version are:
- Node 6.11.2 ([first node version supported by Azure Functions](https://github.com/Azure/azure-webjobs-sdk-script/issues/2036#issuecomment-336942961))
- Node 8 (LTS)
- Node 10
Azure Functions runtime v1 and v2 beta are both supported.
[](LICENSE) © Yves Merlicco