xhtml和html不是同一种文件。在服务器那块,我是根据ajax的setResponse来设置这个返回那个页面。
游览器:
var xhr= new XMLHttpRequest(),
method = "GET",
url = "http://127.0.0.1:8088/";
xhr.open(method, url, true);
xhr.addEventListener('load',()=>{
//console.log(xhr.response);
});
// xhr.responseType = 'DomString';
//xhr.overrideMimeType('text/html');
xhr.setRequestHeader('Content-Type','text/xml');
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr.responseText);
console.log(xhr.responseType);
console.log(xhr.responseXML);
}
};
//xhr.withCredentials = true;
xhr.send();
服务器:
const http = require("http");
const fs = require("fs");
const url = require("url");
const querystring = require("querystring");
let data = '成功开启服务';
fs.writeFile('文件.txt', data, 'utf8',(err) => {
if (err) throw err;
console.log('日志已经输出');
});
let server = http.createServer(function (req,res) {
let arr = req.url.toString().split('/');
let arr1 = __dirname.toString().split('\\');
const UrlArr = arr1.join('/');
if(req.url === '/') {
console.log(req.headers);
if(req.headers["content-type"] ==='text/xml'){
fs.readFile('./asd.xhtml', (err, data) => {
res.setHeader("access-control-allow-origin","*");
res.setHeader('Access-Control-Allow-Headers','*');
res.writeHead(200, {
'Content-Type': 'text/xml'
});
res.write(data,'document');
res.end();
})
}else{
fs.readFile('./server.html', (err, data) => {
res.setHeader("access-control-allow-origin","*");
res.setHeader('Access-Control-Allow-Headers','*');
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(data,'utf8');
res.end();
})
}
}
else if(arr[1] === 'css'){
fs.readFile(`${UrlArr}${req.url}`, (err, data) => {
// res.setHeader("access-control-allow-origin","*");
// res.setHeader('Access-Control-Allow-Headers','*');
res.writeHead(200, {
'Content-Type': 'text/css'
});
res.write(data, 'utf8');
res.end();
})
}
else if (arr[1] === 'js'){
fs.readFile(`${UrlArr}${req.url}`, (err, data) => {
// res.setHeader("access-control-allow-origin","*");
// res.setHeader('Access-Control-Allow-Headers','*');
res.writeHead(200, {
'Content-Type': '*/*',
});
res.write(data, 'document');
res.end();
})
}
else if (arr[1] === 'images'){
fs.readFile(`${UrlArr}${req.url}`, (err, data) => {
// res.setHeader("access-control-allow-origin","*");
// res.setHeader('Access-Control-Allow-Headers','*');
res.writeHead(200, {
'Content-Type': 'image/*'
});
res.write(data);
res.end("结束");
})
}
});
server.listen(8088,'127.0.0.1',()=>{
console.log('服务器开始监听!');
});
。