当前位置:网站首页>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)
边栏推荐
- SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
- What is the function of the JSP out.println() method?
- JSP out.print()和out.write()方法的不同之处
- Redis-消息发布订阅
- 实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。
- 玩转NFT夏季:这份工具宝典值得收藏
- go语言标准库fmt包怎么使用
- JSP如何使用page指令让JSP文件支持中文编码呢?
- Play NFT summer: this collection of tools is worth collecting
- [Headline] Written test questions - minimum stack
猜你喜欢

Unknown CMake command "add_action_files"

How to find new potential projects?Tools recommended

GetHashCode方法与=

Redis - message publish and subscribe

Disk and file system management

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

TCL:在Quartus中使用tcl脚本语言进行管脚约束

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

Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)

08-SDRAM: Summary
随机推荐
Unknown CMake command "add_action_files"
构造方法,this关键字,方法的重载,局部变量与成员变量
What is the function of the JSP out.println() method?
Redis - message publish and subscribe
回顾历史5次经济衰退时期:这一次可能会有何不同?
已知中序遍历数组和先序遍历数组,返回后序遗历数组
2022/08/01 Study Notes (day21) Generics and Enums
08-SDRAM: Summary
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
nodeJs--mime模块
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
uni-app项目总结
Ansible中的任务执行控制
PHP to read data from TXT file
短视频SEO搜索运营获客系统功能介绍
CRS management and maintenance
c语言字符和字符串函数总结(二)
els block boundary deformation processing
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
What is it like to trade for a living?