How to parse Multi-part form data in an Azure Function App with HTTP Trigger? (NodeJS)

安稳与你 提交于 2019-12-06 06:07:13

问题


I want to write a NodeJS HTTP endpoint using Azure Functions.

This endpoint will be a POST endpoint which takes files and upload these to blob storage.

However, NodeJS multipart form data parsers are all in the form of httpserver or expressJS middleware.

Is there any available tools that can parse the multipart form data after it has all been received from the Function Application's wrapper?

Thanks!


回答1:


As Azure Functions has wrapped http server object in Node.js, and exposes a simple req and context with several functionalities, refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#exporting-a-function for details.

And mostly, Azure Functions is designed for triggers and webhooks requests, you can refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-compare-logic-apps-ms-flow-webjobs for the detailed comparison.

Meanwhile, you can try the answer of Image upload to server in node.js without using express to parse the request body content to file content, and upload to Azure Storage leveraging Azure Storage SDK for node.js, you can install custom node modules via KUDU console. Refer to https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node#node-version--package-management for more info.

And I suggest you can try to leverage Azure API App in node.js to approach your requiremnet. As it is an expressjs based project, which will be more easier to handle upload files.

Any further concern, please feel free to let me know.




回答2:


You can try to use this adapter for functions and express, it may allow you to successfully use the multi-part middleware you want: https://github.com/yvele/azure-function-express

As a less desirable option, you can parse the body yourself, all the multi-part data will be available in req.body and will look something like this:

------WebKitFormBoundarymQMaH4AksAbC8HRW
Content-Disposition: form-data; name="key"

value
------WebKitFormBoundarymQMaH4AksAbC8HRW
Content-Disposition: form-data; name=""


------WebKitFormBoundarymQMaH4AksAbC8HRW--

I do think it's a good idea to support httpserver / express better in order to enable this extensibility.




回答3:


To answer original question:

However, NodeJS multipart form data parsers are all in the form of httpserver or expressJS middleware.

Is there any available tools that can parse the multipart form data after it has all been received from the Function Application's wrapper?

Even 2 years later after you asked this question state of multipart form data parsers is not great, like you noticed majority of them assume req object which is a stream and tutorials/demos show how to parse multipart/form-data with express or httpServer.

However there is a parse-multipart npm package which can process req.body from azure function and return you array of objects with code similar to following:

var multipart = require("parse-multipart");


module.exports = function (context, request) {  
    context.log('JavaScript HTTP trigger function processed a request.'); 
    // encode body to base64 string
    var bodyBuffer = Buffer.from(request.body);

    var boundary = multipart.getBoundary(request.headers['content-type']);
    // parse the body
    var parts = multipart.Parse(bodyBuffer, boundary);
    context.res = { body : { name : parts[0].filename, type: parts[0].type, data: parts[0].data.length}}; 
    context.done();  
}; 

(original source: https://www.builtwithcloud.com/multipart-form-data-processing-via-httptrigger-using-nodejs-azure-functions/)

One area where I noticed parse-multipart can struggle is parsing forms with text fields. A slightly improved version which handles it better is called multipart-formdata:

require('multipart-formdata').parse(req.body, boundary)
//returns [{field, name, data, filename, type}, ...] where data is buffer you can use to save files


来源:https://stackoverflow.com/questions/40713479/how-to-parse-multi-part-form-data-in-an-azure-function-app-with-http-trigger-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!