[英]Returning HTML with Azure Serverless Function req.body
我在Azure Blob存儲中有一些TIF文件。 我想通過電子表格中嵌入的鏈接在瀏覽器中顯示它們。 最簡單的方法應該是將文件代碼作為請求參數,然后返回格式正確的HTML,對嗎?
因此,現在我可以返回帶有一些HTML的req.body了。 不幸的是,HTML只是在瀏覽器中顯示為字符串。 如何以最少的rigamarole使其呈現為HTML?
這是我的代碼:
if (req.query.blob) {
let blob = req.query.blob;
context.res = {
// status: 200, /* Defaults to 200 */
body: `<object width=200 height=200
data="<baseaddress>/${blob}.tif" type="image/tiff">
<param name="src" value="<baseaddress>/${blob}.tif">
<param name="negative" value="yes">
</object>`
};
}
您需要設置標題以將內容類型指定為HTML,並且響應必須是完整的有效HTML頁面 (帶有<html>
標記,其余部分)。
范例 :
module.exports.hello = (event, context, callback) => {
const html = `
<!doctype html>
<html>
<head>
<title>The Page Title</title>
</head>
<body>
<h1>Hello</h1>
</body>
</html>`;
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/html'
},
body: html
};
callback(null, response);
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.