npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

larvitreqparser

v0.5.109

Published

Middleware to larvitbase or Express to handle forms

Downloads

1,368

Readme

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