当前位置:网站首页>meime模块
meime模块
2022-08-02 03:22:00 【cjx177187】
var re=mime.getExtension("text/css")
console.log(re)====>打印的是文本类型css
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)
//把前端传过来的数据 去处理
res.end("hello")
}
else if(pathname=="/car"){
res.end("666666car")
}
else {
res.end("404 not found")
}
})
})
app.listen(8080)
代码优化,引入mime
利用mime中的getType返回文件的类型用来取代setHeader中的文本类型,无论什么文件都可以正确读取
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)
//把前端传过来的数据 去处理
res.end("hello")
}
else if(pathname=="/car"){
res.end("666666car")
}
else {
res.end("404 not found")
}
})
})
app.listen(8080)
边栏推荐
猜你喜欢
mysql8.0安装教程与配置(最详细)操作简单
MySQL分组后取最大一条数据【最优解】
Knowledge Engineering Assignment 2: Introduction to Knowledge Engineering Related Fields
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
The @autowired distinguished from @ the Resource
Redis简单学习笔记
Phospholipid-polyethylene glycol-azide, DSPE-PEG-Azide, DSPE-PEG-N3, MW: 5000
MySQL中字符串比较大小(日期字符串比较问题)
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
啃瓜记录又一天
随机推荐
DOM操作---放大镜案例
pyppeteer使用样例脚本
Source Insight 使用教程(2)——常用功能
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
oracle内连接和外连接
@Autowired注解的使用
Deveco studio Hongmeng app access network detailed process (js)
SOCKS5
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group
require模块化语法
第一篇博客
kettle 安装与配置
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰活性基团
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
配置mmdet来训练Swin-Transformer之一配置环境
ModuleNotFoundError No module named 'xxx' possible solutions
【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?
Basic usage of Monaco Editor
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
MySQL分组后排序