当前位置:网站首页>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)
边栏推荐
猜你喜欢

bgp aggregation reflector federation experiment

实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。

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

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

A simple file transfer tools

一文概览最实用的 DeFi 工具

Short video SEO search operation customer acquisition system function introduction

Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)

nodeJs--mime模块
![[头条]笔试题——最小栈](/img/67/08f2be8afc780e3848371a1b5e04db.png)
[头条]笔试题——最小栈
随机推荐
磁盘与文件系统管理
已知中序遍历数组和先序遍历数组,返回后序遗历数组
Ansible中的任务执行控制
uni-app project summary
基于超参数自动寻优的工控网络入侵检测
bgp 聚合 反射器 联邦实验
Transient Stability Distributed Control of Power System with External Energy Storage
一文概览最实用的 DeFi 工具
What is the function of the JSP out.println() method?
08-SDRAM:汇总
测试点等同于测试用例吗
els 方块边界变形处理
基于注意力机制的多特征融合人脸活体检测
扑克牌问题
Business test how to avoid missing?
基于编码策略的电网假数据注入攻击检测
路由策略
An interesting project--Folder comparison tool (1)
Unknown CMake command “add_action_files“
go语言标准库fmt包怎么使用