当前位置:网站首页>nodeJs--mime module
nodeJs--mime module
2022-08-02 00:32:00 【H5_ljy】
Article table of contents
1. What is the mime module
mime is an Internet standard that lets you set how files are opened in browsers.
It is a third-party module and needs to be downloaded manually.
Second, how to install the mime module
Use npm to install the module
npm -i mime //To download to the global, add -g

installationAfter completion, the node_modules and json files will appear in the project, indicating that the installation is successful; then you can use require to import it
Three, mime module method
1.mime.getType(url)
The resource type can be returned through the path
You can add a content-type to the resource (html, css, etc.) requested by the website
No content-type is added to use advanced browsers without error, but low-level browsers mayerror
var http = require('http')var url = require('url')var mime = require('mime')var fs = require('fs')http.createServer((req, res) => {let pathname = url.parse(req.url).pathnamelet type = mime.getType(req.url)console.log(type)fs.readFile(__dirname + pathname, (err, data) => {if (!err) {res.setHeader("content-Type", type)res.end(data)}})}).listen(8081)When I visit the test.txt document
2.mime.getExtension(type)
The query file name can be used with getType, and the MIME type is passed in
var mime = require('mime')var type=mime.getType("./img/test.txt")let type2=mime.getExtension(type)console.log(type,type2)
3.mime.define
mime custom type, when the mime-db library that comes with the mime module does not exist or cannot meet the MIME type we need, you can also customize the MIME type
var mime = require('mime')mime.define({'text/mytext': ['t-txt', 't-ext', 't-xt'],});type=mime.getExtension('text/mytext')console.log(type)
边栏推荐
猜你喜欢
随机推荐
基于注意力机制的多特征融合人脸活体检测
When Netflix's NFTs Forget Web2 Business Security
以交易为生是一种什么体验?
460. LFU cache
ROS dynamic parameters
含外部储能的电力系统暂态稳定分布式控制
06-SDRAM : SDRAM control module
辨析内存函数memset、memcmp、memmove以及memcpy
路由策略
基于编码策略的电网假数据注入攻击检测
JSP built-in object out object function introduction
Day11 shell脚本基础知识
Collection of NFT tools
[HCIP] BGP Small Experiment (Federation, Optimization)
What is the function of the JSP out.println() method?
els 方块变形
信息物理系统状态估计与传感器攻击检测
How to use the go language standard library fmt package
IP Core: FIFO
The Statement update Statement execution


![[头条]笔试题——最小栈](/img/67/08f2be8afc780e3848371a1b5e04db.png)






