当前位置:网站首页>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)
边栏推荐
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- An overview of the most useful DeFi tools
- ROS dynamic parameters
- JSP out. The write () method has what function?
- [头条]笔试题——最小栈
- Task execution control in Ansible
- 信息物理系统状态估计与传感器攻击检测
- JSP how to obtain the path information in the request object?
- After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
- els strip deformation
猜你喜欢
Axure tutorial - the new base (small white strongly recommended!!!)
146. LRU 缓存
字符串分割函数strtok练习
Short video seo search optimization main content
22. The support vector machine (SVM), gaussian kernel function
回顾历史5次经济衰退时期:这一次可能会有何不同?
测试用例:四步测试设计法
Identify memory functions memset, memcmp, memmove, and memcpy
How to find new potential projects?Tools recommended
Using the "stack" fast computing -- reverse polish expression
随机推荐
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
中缀转后缀、前缀表达式快速解决办法
如何发现新的潜力项目?工具推荐
一文概览最实用的 DeFi 工具
已知中序遍历数组和先序遍历数组,返回后序遗历数组
接地气讲解TCP协议和网络程序设计
磁盘与文件系统管理
JSP out.write()方法具有什么功能呢?
Redis-消息发布订阅
JSP out.print()和out.write()方法的不同之处
Industrial control network intrusion detection based on automatic optimization of hyperparameters
632. Minimum interval
Redis - message publish and subscribe
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
NFT工具合集
08-SDRAM:汇总
利用“栈”快速计算——逆波兰表达式
鲲鹏编译调试插件实战
IP Core: FIFO
Constructor, this keyword, method overloading, local variables and member variables