标签:list end ipa script cte art one == div
// 引入http模块
let http = require(‘http‘);
// 引入url模块 获取域名后面的url
let path = require(‘path‘);
let fs = require(‘fs‘);
let url = require(‘url‘);
// 创建http服务
// request 获取客户端传过来的信息,response 给浏览器响应的信息
http.createServer((request, response) => {
console.log(request.url);
// 有些地址后面会有传值 列入/index.html?12324123
// url.parse(request.url).pathname url.parse().pathname去掉request.url后面的传值
let queryUrl = url.parse(request.url).pathname === ‘/‘ ? ‘/index.html‘ : url.parse(request.url).pathname;
// extname 获取文件的后缀名(格式名)
let extname = path.extname(queryUrl);
console.log(extname);
console.log(getContentType(extname));
// 设置响应头
response.writeHead(200,{‘Content-Type‘: getContentType(extname)});
response.write(‘<head><meta charset="UTF-8"></head>‘);
// 不请求favicon.ico
response.write(‘<link rel="icon" href="data:image/ico;base64,aWNv">‘);
fs.readFile(`./components${queryUrl}`, (err, data) => {
if (err) {
response.end(‘页面404,不存在这个页面‘);
console.log(err); return;
}
// console.log(data.toString());
response.end(data.toString());
})
}).listen(8888);
console.log(‘httpServer http://127.0.0.1:8888‘);
// 根据文件类型 设置Content-Type
function getContentType(type) {
switch (type) {
case ‘.html‘:
return ‘text/html;charset=UTF-8‘;
case ‘.css‘:
return ‘text/css;charset=UTF-8‘;
case ‘.js‘:
return ‘text/javascript;charset=UTF-8‘;
case ‘.json‘:
return ‘application/json;charset=UTF-8‘;
case ‘.pdf‘:
return ‘application/pdf;charset=UTF-8‘;
case ‘.doc‘:
return ‘application/msword;charset=UTF-8‘;
case ‘.rtf‘:
return ‘application/msword;charset=UTF-8‘;
case ‘.octet-stream‘:
return ‘application/octet-stream;charset=UTF-8‘;
case ‘.form-data‘:
return ‘multipart/form-data;charset=UTF-8‘;
case ‘.gif‘:
return ‘image/gif;charset=UTF-8‘;
case ‘.jpeg‘:
return ‘image/jpeg;charset=UTF-8‘;
case ‘.png‘:
return ‘image/png;charset=UTF-8‘;
}
}
标签:list end ipa script cte art one == div
原文地址:https://www.cnblogs.com/zhaofeis/p/12811530.html