• N6_287628
  • 2.3MB
  • zip
  • 0
  • VIP专享
  • 0
  • 2022-04-22 18:12
:rocket: 节点 :memo: 使用Node.js构建东西 :right_arrow: 内容 :keycap_1: :keycap_2:
<p align="center"> <img alt="npm formidable package logo" src="" /> </p> # formidable [![npm version][npmv-img]][npmv-url] [![MIT license][license-img]][license-url] [![Libera Manifesto][libera-manifesto-img]][libera-manifesto-url] [![Twitter][twitter-img]][twitter-url] > A Node.js module for parsing form data, especially file uploads. ### Important Notes - This README is for the upcoming (end of February) v2 release! - Every version prior and including `v1.2.2` is deprecated, please upgrade! - Install with `formidable@canary` until v2 land officially in `latest` - see more about the changes in the []( [![Code style][codestyle-img]][codestyle-url] [![codecoverage][codecov-img]][codecov-url] [![linux build status][linux-build-img]][build-url] [![windows build status][windows-build-img]][build-url] [![macos build status][macos-build-img]][build-url] If you have any _how-to_ kind of questions, please read the [Contributing Guide][contributing-url] and [Code of Conduct][code_of_conduct-url] documents.<br /> For bugs reports and feature requests, [please create an issue][open-issue-url] or ping [@tunnckoCore]( at Twitter. [![Conventional Commits][ccommits-img]][ccommits-url] [![Minimum Required Nodejs][nodejs-img]][npmv-url] [![Tidelift Subcsription][tidelift-img]][tidelift-url] [![Buy me a Kofi][kofi-img]][kofi-url] [![Renovate App Status][renovateapp-img]][renovateapp-url] [![Make A Pull Request][prs-welcome-img]][prs-welcome-url] This project is [semantically versioned]( and available as part of the [Tidelift Subscription][tidelift-url] for professional grade assurances, enhanced support and security. [Learn more.]( _The maintainers of `formidable` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the Open Source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use._ [![][npm-weekly-img]][npmv-url] [![][npm-monthly-img]][npmv-url] [![][npm-yearly-img]][npmv-url] [![][npm-alltime-img]][npmv-url] ## Status: Maintained [![npm version][npmv-canary-img]][npmv-url] This module was initially developed by [**@felixge**]( for [Transloadit](, a service focused on uploading and encoding images and videos. It has been battle-tested against hundreds of GBs of file uploads from a large variety of clients and is considered production-ready and is used in production for years. Currently, we are few maintainers trying to deal with it. :) More contributors are always welcome! :heart: Jump on [issue #412]( which is closed, but if you are interested we can discuss it and add you after strict rules, like enabling Two-Factor Auth in your npm and GitHub accounts. _**Note:** The github `master` branch is a "canary" branch - try it with `npm i formidable@canary`. Do not expect (for now) things from it to be inside the`latest` "dist-tag" in the Npm. The`formidable@latest`is the`v1.2.1` version and probably it will be the last`v1` release!_ _**Note: v2 is coming soon!**_ ## Highlights - [Fast (~900-2500 mb/sec)](#benchmarks) & streaming multipart parser - Automatically writing file uploads to disk (soon optionally) - [Plugins API](#useplugin-plugin) - allowing custom parsers and plugins - Low memory footprint - Graceful error handling - Very high test coverage ## Install This project requires `Node.js >= 10.13`. Install it using [yarn]( or [npm](<br /> _We highly recommend to use Yarn when you think to contribute to this project._ ```sh npm install formidable # or the canary version npm install formidable@canary ``` or with Yarn v1/v2 ```sh yarn add formidable # or the canary version yarn add formidable@canary ``` This is a low-level package, and if you're using a high-level framework it _may_ already be included. Check the examples below and the `examples/` folder. ## Examples For more examples look at the `examples/` directory. ### with Node.js http module Parse an incoming file upload, with the [Node.js's built-in `http` module]( ```js const http = require('http'); const formidable = require('formidable'); const server = http.createServer((req, res) => { if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') { // parse a file upload const form = formidable({ multiples: true }); form.parse(req, (err, fields, files) => { res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify({ fields, files }, null, 2)); }); return; } // show a file upload form res.writeHead(200, { 'content-type': 'text/html' }); res.end(` <h2>With Node.js <code>"http"</code> module</h2> <form action="/api/upload" enctype="multipart/form-data" method="post"> <div>Text field title: <input type="text" name="title" /></div> <div>File: <input type="file" name="multipleFiles" multiple="multiple" /></div> <input type="submit" value="Upload" /> </form> `); }); server.listen(8080, () => { console.log('Server listening on http://localhost:8080/ ...'); }); ``` ### with Express.js There are multiple variants to do this, but Formidable just need Node.js Request stream, so something like the following example should work just fine, without any third-party [Express.js]( middleware. Or try the [examples/with-express.js]( ```js const express = require('express'); const formidable = require('formidable'); const app = express(); app.get('/', (req, res) => { res.send(` <h2>With <code>"express"</code> npm package</h2> <form action="/api/upload" enctype="multipart/form-data" method="post"> <div>Text field title: <input type="text" name="title" /></div> <div>File: <input type="file" name="someExpressFiles" multiple="multiple" /></div> <input type="submit" value="Upload" /> </form> `); });'/api/upload', (req, res, next) => { const form = formidable({ multiples: true }); form.parse(req, (err, fields, files) => { if (err) { next(err); return; } res.json({ fields, files }); }); }); app.listen(3000, () => { console.log('Server listening on http://localhost:3000 ...'); }); ``` ### with Koa and Formidable Of course, with [Koa v1, v2 or future v3]( the things are very similar. You can use `formidable` manually as shown below or through the [koa-better-body]( package which is using `formidable` under the hood and support more features and different request bodies, check its documentation for more info. _Note: this example is assuming Koa v2. Be aware that you should pass `ctx.req` which is Node.js's Request, and **NOT** the `ctx.request` which is Koa's Request object - there is a difference._ ```js const Koa = require('koa'); const formidable = require('formidable'); const app = new Koa(); app.on('error', (err) => { console.error('server error', err); }); app.use(async (ctx, next) => { if (ctx.url === '/api/upload' && ctx.method.toLowerCase() === 'post') { const form = formidable({ multiples: true }); // not very elegant, but that's for now if you don't want touse `koa-better-body` // or other middlewares. await new Promise((resolve, reject) => { form.parse(ctx.req, (err, fields, files) => { if (err) { reject(err);
    • node.js:Node.js
      node.js 如果发现错误,请将其保存在并提出拉取请求。 谢谢!
    • node.js:学习 Node.js
      节点.js 学习 Node.js :D
    • node.js开发指南
    • Node.js 7.x异步/等待支持演示-Node.js开发
      Node.js v7 async / await演示这是什么? Node.js v7.xx附带实验性的异步/等待支持,大大简化了编写现代服务器端代码所需的样板。 该项目是Node.js v7 async / await演示的演示。这是什么? Node.js v7.xx附带实验性...
    • node.js
      node.js !!!
    • node.js
      node.js 실습 Node.js版本-기프로젝트지
    • node.js
    • node.js
    • node.js:node.js学习
      node.js node.js研究
    • Node.js