New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

miming

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

miming

Processing and formatting for various mime types.

latest
Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
12
1100%
Maintainers
1
Weekly downloads
 
Created
Source

Miming

Build Status

A very small library for parsing and formatting various different mime types in node.js.

Note: Miming does not deal with content negotiation, you should use another library such as negotiator for that.

Miming supports the following built in mime types:

Installation

Via npm:

npm install miming

Usage

Creating a collection

Create a Collection of MimeTypes that you'd like to handle in your application. Specify the types in order of precedence.

var Miming = require('miming');

var collection = new Miming.Collection([
  'HTML',
  'JSON',
  'FormURLEncoded',
  'MultipartFormData'
]);

var mime = collection.get('application/json');
mime.should.be.an.instanceOf(Miming.Types.JSON);

console.log(mime.format({foo: 'bar'})).should.equal('{"foo":"bar"}');

Adding a type to a collection

collection.add('JSONLD');
// or
collection.add('application/json');
// or
collection.add(new Miming.Types.JSONLD());
// or
collection.add(Miming.Types.JSONLD);

Processing requests

Here's a tiny application which parses requests in various formats, translates them to JSON, and echos them back to the client.

var http = require('http'),
    Negotiator = require('negotiator'),
    Miming = require('../lib'),
    collection = new Miming.Collection();

collection.add('application/json');
collection.add('application/x-www-form-urlencoded');
collection.add('multipart/form-data');


http.createServer(function (req, res) {
  if (!req.headers['content-type']) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end('<form method="POST"><textarea name="content"></textarea><br><button type="submit">Go</button></form>');
    return;
  }
  collection.parse(req)
  .spread(function (body, files) {
    res.writeHead(200, {'Content-Type': 'application/json'});
    return collection.format('application/json', {
      body: body,
      files: files
    })
    .then(res.end.bind(res));
  })
  .done();
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

License

MIT, see LICENSE.md.

Keywords

mime-type

FAQs

Package last updated on 19 Jun 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts