当前位置:网站首页>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)
边栏推荐
- basic operator
- A senior test engineer asked me these questions as soon as the interview came
- 小程序 van-cell 换行能左对齐问题
- Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
- Chapter 10 Clustering
- 微信小程序实现文本安全监测
- COCO数据集训练TPH-YoloV5
- page load process
- Common methods of js array deduplication
- 【装机】老毛桃的安装及使用
猜你喜欢

ModuleNotFoundError No module named 'xxx' possible solutions

知识工程作业2:知识工程相关领域介绍

微信小程序云开发如何将页面生成为pdf?

Debian 10 NTP Service Configuration

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置

Redis简单学习笔记

云服务器安装部署Nacos2.0.4版本

【手把手带你学nRF52832/nRF52840 · (1)开发环境搭建】

URL模块
随机推荐
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
__dirname
Dynamic proxy tool class
ssm various configuration templates
UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
--fs模块--
sh: 1: curl: not found
云服务器安装部署Nacos2.0.4版本
由中序遍历和后序遍历得到前序遍历(树的遍历)
跨域问题解决
C语言 结构体定义方法
DOM操作---放大镜案例
npm--package.json---require
我的小笔记 =》其他东东
微信小程序云开发如何将页面生成为pdf?
C语言入门小游戏—三子棋
C语言 0长度数组/柔性数组
Advanced gradient of skeleton effect, suitable for waiting for pictures
js basics
新工程加载YOLOV6的预训练权重问题