0.5.81 • Published 9 days ago

larvitreqparser v0.5.81

Weekly downloads
58
License
ISC
Repository
github
Last release
9 days ago

Build Status

Request parser middleware

Middleware for larvitbase or express to handle parsing url, forms and file uploads. This is just a wrapper for the following libraries:

As a little bonus, it is also setting a request uuid to identify every request in logs etc.

Installation

npm i --save larvitreqparser

Usage

Larvitbase

Usage with larvitbase

const App = require('larvitbase');
const ReqParser = require('larvitreqparser');
const reqParser = new ReqParser({
	// OPTIONAL
	'fs': require('fs-extra'), // Needs some extra functions from fs-extra
	'log': new (require('larvitutils').Log(), // Compatible with the winston logging library
	'storage': 'memory', // Default. Options: 'memory' or a file path, for example '/tmp'.
	'busboyOptions': {} // Custom busboy options, see https://github.com/mscdex/busboy for options
});

new App({
	'httpOptions': 8001,
	'middleware': [
		reqParser.parse.bind(reqParser), // We must bind() the context or we'll lose it
		function (req, res) {
			// Now the following properties is populated depending on the request type:

			// req.urlParsed - URL parsed by require('url').parse()

			// Will be populated when a HTML form is posted either as multipart or as default html form.
			// req.formFields

			// Will be populated when a HTML multipart form is posted with files
			// !!! NOT when sending just a single file as body, that will only populate req.rawBody (see below)
			// req.formFiles[fieldName].filename
			// req.formFiles[fieldName].mimetype
			// req.formFiles[fieldName].encoding

			// If storage === 'memory'
			// req.rawBody
			// req.formFiles[fieldName].buffer (Only when multipart form is posted)

			// If storage is path on disk
			// req.rawBodyPath
			// req.formFiles[fieldName].path (Only when multipart form is posted)

			res.end('Hello world');
		}
	]
});

Cleanup when not using memory

When not using memory, files are stored on disk. They must be manually removed or they will just fill up infinitly!

const App = require('larvitbase');
const ReqParser = require('larvitreqparser');
const reqParser = new ReqParser({
	'storage': '/tmp'
});
const fs = require('fs');

new App({
	'httpOptions': 8001,
	'middleware': [
		reqParser.parse,
		function (req, res, cb) {
			res.end('Hello world');
			cb();
		},
		reqParser.clean
	]
});

If a file should not be cleaned up for some reason a flag can be set on the formFile object to indicate manual cleanup:

function middleware(req, res, cb) {
	req.formFiles.myFile.manualCleanup = true; // Tell larvitreqparser clean function not to remove the file

	doSomethingAsyncWithTheFile(req.formFiles.myFile, function() {
		// Manually remove the file when we are done with it
	});

	cb(); // We continue before async work on the file is completed
}

Changelog

  • 0.4.7 - Added simplest possible declaration file for package to work with typescript
0.5.81

9 days ago

0.5.80

16 days ago

0.5.79

23 days ago

0.5.78

1 month ago

0.5.77

1 month ago

0.5.76

2 months ago

0.5.75

2 months ago

0.5.74

2 months ago

0.5.73

3 months ago

0.5.72

3 months ago

0.5.71

3 months ago

0.5.70

4 months ago

0.5.69

4 months ago

0.5.68

4 months ago

0.5.67

4 months ago

0.5.66

5 months ago

0.5.65

5 months ago

0.5.64

5 months ago

0.5.63

5 months ago

0.5.59

6 months ago

0.5.61

6 months ago

0.5.62

6 months ago

0.5.60

6 months ago

0.5.58

7 months ago

0.5.54

8 months ago

0.5.55

7 months ago

0.5.52

8 months ago

0.5.53

8 months ago

0.5.51

8 months ago

0.5.56

7 months ago

0.5.57

7 months ago

0.5.50

9 months ago

0.5.43

1 year ago

0.5.44

1 year ago

0.5.49

10 months ago

0.5.47

11 months ago

0.5.48

11 months ago

0.5.45

12 months ago

0.5.46

11 months ago

0.5.41

1 year ago

0.5.42

1 year ago

0.5.40

1 year ago

0.5.32

1 year ago

0.5.33

1 year ago

0.5.31

1 year ago

0.5.38

1 year ago

0.5.39

1 year ago

0.5.36

1 year ago

0.5.37

1 year ago

0.5.34

1 year ago

0.5.35

1 year ago

0.5.30

1 year ago

0.5.21

2 years ago

0.5.22

2 years ago

0.5.20

2 years ago

0.5.29

1 year ago

0.5.27

1 year ago

0.5.28

1 year ago

0.5.25

1 year ago

0.5.26

1 year ago

0.5.23

2 years ago

0.5.24

1 year ago

0.5.18

2 years ago

0.5.19

2 years ago

0.5.16

2 years ago

0.5.17

2 years ago

0.5.15

2 years ago

0.5.10

2 years ago

0.5.11

2 years ago

0.5.8

2 years ago

0.5.7

2 years ago

0.5.9

2 years ago

0.5.14

2 years ago

0.5.12

2 years ago

0.5.13

2 years ago

0.5.6

2 years ago

0.5.5

2 years ago

0.5.4

2 years ago

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

4 years ago

0.4.10

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

6 years ago

0.2.2

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago