1 const http = require('http'); 2 const url = require('url'); 3 const path = require('path'); 4 const fs = require('fs'); 5 const mime=require('mime'); 6 7 8 const app = http.createServer(); 9 app.on('request',(req,res)=>{10 11 //获取用户请求路劲12 let pathname=url.parse(req.url).pathname;13 14 pathname = pathname== '/'? '/default.html':pathname;15 //将用户的请求路径转换为实际的服务器路径16 let realPath=path.join(__dirname,'public'+pathname);17 18 // 当前请求文件类型19 // console.log(mime.getType(realPath)); 20 let type = mime.getType(realPath);21 //读取文件22 fs.readFile(realPath, (error,result)=>{23 if(error != null){24 res.writeHead(400, {25 'content-type': 'text/html;charset=utf8'26 });27 res.end('文件读取失败');28 return;29 }30 res.writeHead(200,{31 'content-type':type32 })33 res.end(result)34 })35 })36 37 app.listen(3000);38 console.log('服务器启动成功');
原文转载:http://www.shaoqun.com/a/485861.html
敦煌网:https://www.ikjzd.com/w/189
声网:https://www.ikjzd.com/w/2176
克雷格:https://www.ikjzd.com/w/194
1consthttp=require('http');2consturl=require('url');3constpath=require('path');4constfs=require('fs');5constmime=require('mime');678constapp=http.createServer()
tradekey:https://www.ikjzd.com/w/1630
跨境通电子商务网站:https://www.ikjzd.com/w/1329
珠海东澳岛好玩吗?:http://tour.shaoqun.com/a/2805.html
《上海市促进在线新经济发展行动方案(2020—2022年):http://tour.shaoqun.com/a/35830.html
【去南非旅游最佳季节】--什么季节适合去南非旅游:http://tour.shaoqun.com/a/26404.html
No comments:
Post a Comment