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':type 32 }) 33 res.end(result) 34 }) 35 }) 36 37 app.listen(3000); 38 console.log('服務器啟動成功');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/197194.html
標籤:其他
上一篇:Vuex 部分
