当前位置:网站首页>nodeJs--mime模块
nodeJs--mime模块
2022-08-02 00:05:00 【H5_ljy】
一、mime模块是什么
mime是一个互联网标准,通过设定它就可以设定文件在浏览器的打开方式。
它是第三方模块,需要手动下载。
二、如何安装mime模块
使用npm安装模块
npm -i mime //下载到全局则要加-g

安装完后项目中会出现node_modules和json文件说明安装成功;接下来就可以使用require来引入它了
三、mime模块方法
1.mime.getType(url)
可以通过路径返回资源类型
可以给网站请求的资源(html、css等)添加一个content-type
没有添加content-type使用高级浏览器不会出错,但低级浏览器可能会出错
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).pathname
let 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)
当我访问test.txt文档时
2.mime.getExtension(type)
查询文件护展名可配合getType使用,传入的是MIME类型
var mime = require('mime')
var type=mime.getType("./img/test.txt")
let type2=mime.getExtension(type)
console.log(type,type2)

3.mime.define
mime自定义类型,当mime模块自带的mime-db库不存在或不能满足我们所需的MIME类型时,还可以自定义MIME类型
var mime = require('mime')
mime.define({
'text/mytext': ['t-txt', 't-ext', 't-xt'],
});
type=mime.getExtension('text/mytext')
console.log(type)

边栏推荐
- Unknown CMake command “add_action_files“
- JSP内置对象out对象的功能简介说明
- 微软电脑管家V2.1公测版正式发布
- The Statement update Statement execution
- JSP out.println()方法具有什么功能呢?
- 【21天学习挑战赛】顺序查找和二分查找的小总结
- 【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
- QML包管理
- Async/await principle and execution sequence analysis
- 短视频SEO搜索运营获客系统功能介绍
猜你喜欢

security cross-domain configuration

短视频seo搜索优化主要内容

【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core

为什么要使用MQ消息中间件?这几个问题必须拿下

22.支持向量机—高斯核函数

【三子棋】C语言实现简易三子棋

面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值

【HCIP】BGP小型实验(联邦,优化)

已知中序遍历数组和先序遍历数组,返回后序遗历数组

An interesting project--Folder comparison tool (1)
随机推荐
控制电机的几种控制电路原理图
C language Qixi is coming!It's time to show the romance of programmers!
460. LFU 缓存
Redis-消息发布订阅
632. 最小区间
图解LeetCode——1161. 最大层内元素和(难度:中等)
go语言标准库fmt包怎么使用
JSP out.write()方法具有什么功能呢?
Short video seo search optimization main content
【21天学习挑战赛】顺序查找和二分查找的小总结
接地气讲解TCP协议和网络程序设计
短视频SEO搜索运营获客系统功能介绍
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
The Statement update Statement execution
Cash Ⅱ LeetCode_518_ change
After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
学习英语的网站与资料
How does JSP use the page command to make the JSP file support Chinese encoding?
GetHashCode与Equals
当奈飞的NFT忘记了Web2的业务安全