当前位置:网站首页>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)
边栏推荐
- c语言字符和字符串函数总结(二)
- 业务测试如何避免漏测 ?
- Unknown CMake command "add_action_files"
- Unknown CMake command “add_action_files“
- 08-SDRAM: Summary
- 含外部储能的电力系统暂态稳定分布式控制
- [Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
- Redis的集群模式
- JSP request对象功能详解说明
- An overview of the most useful DeFi tools
猜你喜欢

GetHashCode方法与=

微软电脑管家V2.1公测版正式发布

这 4 款电脑记事本软件,得试试

SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture

Task execution control in Ansible

如何发现新的潜力项目?工具推荐

Axure tutorial - the new base (small white strongly recommended!!!)

GIF making - very simple one-click animation tool

How to design a circular queue?Come and learn~
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search
随机推荐
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ
els 方块变形
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
An interesting project--Folder comparison tool (1)
07-SDRAM: FIFO control module
面试:简单介绍你参与的一个项目
632. 最小区间
22. The support vector machine (SVM), gaussian kernel function
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
Double queue implementation stack?Dual stack implementation queue?
基于超参数自动寻优的工控网络入侵检测
ROS dynamic parameters
Grid false data injection attacks detection based on coding strategy
Disk and file system management
图解LeetCode——1161. 最大层内元素和(难度:中等)
460. LFU cache
[HCIP] BGP Small Experiment (Federation, Optimization)
Redis - message publish and subscribe