当前位置:网站首页>meime module
meime module
2022-08-02 03:39:00 【cjx177187】
var re=mime.getExtension("text/css")
console.log(re)====>prints text typecss
var re2=mime.getType("1.jpg")
console.log(re2)=====>返回jpg
mime.getExtension===>返回类型
mime.getType=====>返回文件类型
var url=require("url")
var querystring=require("querystring")
var http=require("http")
var fs=require("fs")
var app=http.createServer((req,res)=>{
let urlobj=url.parse(req.url)
let pathname=urlobj.pathname
if(pathname=="/"){ pathname="/index.html"}
let path=__dirname+"/src"+pathname
console.log(111111111111,path)
fs.readFile(path,(err,data)=>{
if(!err){
res.setHeader("content-Type","text/html")
res.end(data)
}
else if(pathname=="/login"){
let queryobj=querystring.parse(urlobj.query)
console.log(queryobj)
//Send the data from the front end 去处理
res.end("hello")
}
else if(pathname=="/car"){
res.end("666666car")
}
else {
res.end("404 not found")
}
})
})
app.listen(8080)
代码优化,引入mime
利用mime中的getTypeReturns the type of file to use insteadsetHeader中的文本类型,Any file can be read correctly
var mime=require("mime")
var url=require("url")
var querystring=require("querystring")
var http=require("http")
var fs=require("fs")
var app=http.createServer((req,res)=>{
let urlobj=url.parse(req.url)
let pathname=urlobj.pathname
if(pathname=="/"){ pathname="/index.html"}
let path=__dirname+"/src"+pathname
console.log(111111111111,path)
fs.readFile(path,(err,data)=>{
if(!err){
let type1=mime.getType(path)
res.setHeader("content-Type",type1)
res.end(data)
}
else if(pathname=="/login"){
let queryobj=querystring.parse(urlobj.query)
console.log(queryobj)
//Send the data from the front end 去处理
res.end("hello")
}
else if(pathname=="/car"){
res.end("666666car")
}
else {
res.end("404 not found")
}
})
})
app.listen(8080)
边栏推荐
- meime模块
- nucleo stm32 h743 FREERTOS CUBE MX配置小记录
- np.unique()函数
- DSPE-PEG-DBCO Phospholipid-Polyethylene Glycol-Dibenzocyclooctyne A Linear Heterobifunctional Pegylation Reagent
- SOCKS5
- npm--package.json---require
- Common methods of js array deduplication
- Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
- 利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
- docker中配置mysql 5.7
猜你喜欢
【装机】老毛桃的安装及使用
yolov5调用ip摄像头时出现的问题
STM32 map文件解析
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
querystring模块
Cut out web icons through PS 2021
ModuleNotFoundError No module named 'xxx' possible solutions
js基础知识
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
云服务器安装部署Nacos2.0.4版本
随机推荐
require modular syntax
每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000
Problems when yolov5 calls ip camera
L1-039 古风排版(C)
Source Insight 使用教程(2)——常用功能
猴子选大王(约瑟环问题)
微信小程序云开发之模糊搜索
The difference between the knowledge question and answer session with the knowledge
mysql阶段总结
yolov5调用ip摄像头时出现的问题
FreeRTOS内核详解(1) —— 临界段保护原理
由中序遍历和后序遍历得到前序遍历(树的遍历)
配置mmdet来训练Swin-Transformer之一配置环境
这些JS题面试时一定要答对!
Debian 10 NTP Service Configuration
广州华为面试总结
Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
---静态页面---
暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀