This commit is contained in:
Ian Hollier 2021-02-03 14:00:40 -08:00
Родитель f0acbeea67
Коммит fa886c36d9
5 изменённых файлов: 84 добавлений и 1 удалений

Просмотреть файл

@ -44,5 +44,9 @@ Open your browser of choice and go to the URL http//localhost:8080. You should
## Hosting on Azure
The code can obviously be hosted on any web service but in my case I chose to host using an [Azure function](https://azure.microsoft.com/en-us/services/functions/?&ef_id=CjwKCAiAsOmABhAwEiwAEBR0ZmNO6WIwjimRlpY2W-N4U_G99qJHALIQa-hykDyFhzNSz6bJl3x8nRoCVcYQAvD_BwE:G:s&OCID=AID2100131_SEM_CjwKCAiAsOmABhAwEiwAEBR0ZmNO6WIwjimRlpY2W-N4U_G99qJHALIQa-hykDyFhzNSz6bJl3x8nRoCVcYQAvD_BwE:G:s&gclid=CjwKCAiAsOmABhAwEiwAEBR0ZmNO6WIwjimRlpY2W-N4U_G99qJHALIQa-hykDyFhzNSz6bJl3x8nRoCVcYQAvD_BwE) as it is a cost effective way to host infrequently accessed static web pages.
I used [this blog post](https://www.wintellect.com/host-website-azure-functions-node-js-part-1/) to setup my Azure function. The code needed for the Azure Function can be found in the file [azure-function/index.js]().
## Running the sample

Просмотреть файл

@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}

7
azure-function/host.json Normal file
Просмотреть файл

@ -0,0 +1,7 @@
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
}
}

43
azure-function/index.js Normal file
Просмотреть файл

@ -0,0 +1,43 @@
var fs = require("fs")
var mime = require('mime-types')
module.exports = function (context, req) {
var file="index.html"
if (req.query.file) {
file=req.query.file
}
file = file.replace(/\//g, "\\");
fs.readFile(__dirname + "\\content\\" + file, function(err, data) {
context.log('GET ' + __dirname + "\\content\\" + file);
if (!err){
var contentType = mime.lookup(file)
context.res = {
status: 200,
body: data,
isRaw: true,
headers: {
'Content-Type': contentType
}
};
} else {
context.log("Error: " + err)
context.res = {
status: 404,
body: "Not Found.",
headers: {
}
};
}
context.done()
});
};

Просмотреть файл

@ -0,0 +1,10 @@
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"Page Request": {
"matchCondition": {
"route": "/{*page}"
},
"backendUri": "https://localhost/api/GetPage?file={page}"
}
}