MIME module responds or sets node. JS's Content-type Header

Source: Internet
Author: User

Reprinted from: https://itbilu.com/nodejs/core/VJYaAfKrl.html

  MIME, that is:Multipurpose Internet Mail Extensions, Multi-purpose Internet Message extension type . Its main purpose is to set the responder type of a file with an extension, and we can assume that when the specified extension file is accessed, the browser will automatically open with the specified application. In HTTP, the corresponding file type is Content-Type set or responded to by the HTTP header named. For example, when the server wants to send the content graph class to the client as a. jpg image, the header needs to be Content-Type set to image/jpeg , and the client will also Content-Type parse the server content.

  MIMEAnd Content-Type is the file type setting and the standard for the board. When the server is sending an extension file to the client, the header is set according to the file name extension Content-Type . While the client (which can be considered a browser), parsing the contents of the server also requires the Content-Type parser that represents the content to be MIME found. There are a MIME lot of types, and when we set up the Send content format on the server or when we parse the service-side content, it's a lot of work to MIME do with hundreds of types.

Recommend a NPM package: mime . The mime module uses the Apache project's mime.types file, which contains more than 600 Content-Type types of data, and supports the addition of a custom MIME type.

1. mimeInstallation

mimeThe module is a mime-db type-based MIME parsing and processing program.

Using the npm installation module:

NPM Install MIME

After installation, the mime module tests:

NPM Run Test

mimeAfter the module is installed, it can be used under command line (command line use requires npm install -g global installation) in the following format:

MIME [path_string]

For example, the mime module looks at app.js the type of file in the current directory:

MIME app.js//application/javascript

2. mimeQuery API for modules

The mime module can be used to query the type associated with a file through a file and its extension MIME , or to MIME find the file's extension by type reverse.

2.1 Types of query files mime.lookup(path)

mimeto query the type of file using a module MIME :

var mime = require (' MIME '); Mime.lookup ('/path/to/file.txt ');         = ' Text/plain ' mime.lookup (' file.txt ');                  = ' Text/plain ' Mime.lookup ('. TXT ');                      = ' Text/plain ' mime.lookup (' htm ');                       = ' text/html '

mime.default_typeReturns the default type

mime.lookup()the type is returned when the file type that is found MIME does not exist application/octet-stream . mime.lookup()the path is not case-sensitive when locating the file. Can be mime.default_type returned by returning the default type currently in use.

2.2 Query File Protection exhibition name mime.extension(type)

mimeThe module provides a way to query the file name extension, and we can find the extension of the file by the Content-Type MIME type represented, and finally confirm the content parsing form and the parser.

mime.extension (' text/html ');                 = ' html 'mime.extension (' Application/octet-stream ');  = ' bin '

2.3 Find type encoding mime.charsets.lookup()

mime MIME The code used by the type can be queried by the module:

mime.charsets.lookup (' Text/plain ');        = ' UTF-8 '

3. mimeCustom Types

You mime can also customize the type when the module comes with a mime-db library that does not exist or does not meet the MIME type that we want MIME .

3.1 Custom Types mime.define()
Mime.define (obj)

The method accepts a parameter obj , which is a representation mime类型/扩展名 of the object.

mime.define ({    ' text/x-some-format ': [' x-sf ', ' x-sft ', ' X-sfml '],    ' application/x-my-type ': [' x-mt ', ' X-MTT '],    //etc ...}); mime.lookup (' x-sft ');                 = ' Text/x-some-format '

3.2 Loading type files mime.load(filepath)

mimeThe module supports loading types from an Apache file, and it is easier to mime.types MIME load types from existing files relative to custom types:

Mime.load ('./my_project.types ');


MIME module responds or sets node. JS's Content-type Header

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.