const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
res.end(Date.now() + ' == > http访问成功8080')
});
server.listen(8080, () => {
console.log('服务已开启');
})
const https = require('https');
const fs = require('fs');
const path = require('path');
const options = {
key: fs.readFileSync(path.join(__dirname, './key.pem')),
cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};
const server = https.createServer(options, (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
res.end('https访问8081成功 == > ' + Date.now())
});
server.listen(8081, () => {
console.log('服务已开启');
})
var http = require('http');
var httpProxy = require('http-proxy')
let proxy = httpProxy.createProxyServer()
let server = http.createServer((req, res) => {
proxy.web(req, res, {
target: 'http://localhost:8080',
})
})
server.listen(3000)
server.on('listening', () => {
console.log('http启动完成')
})
server.on('close', () => {
console.log('服务器关闭')
})
let httpProxy = require('http-proxy')
let https = require('https');
const fs = require('fs');
const path = require('path');
const options = {
key: fs.readFileSync(path.join(__dirname, './key.pem')),
cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};
let hosts = {
'as.cc': 'http://localhost:8080',
}
let proxy = httpProxy.createProxyServer()
let server = https.createServer(options, (req, res) => {
let host = req.headers['host'].split(':')[0]
console.log(666.789, host, hosts[host])
proxy.web(req, res, {
target: hosts[host] || 'https://localhost:8081'
})
})
server.listen(3001)
server.on('listening', () => {
console.log('https启动完成')
})
server.on('close', () => {
console.log('服务器关闭')
})
const http = require("http");
const server = http.createServer();
server.on("request", (req, res) => {
var { connection, host, ...originHeaders } = req.headers;
var options = {
"method": req.method,
"hostname": "localhost",
"port": "8080",
"path": req.url,
"headers": { originHeaders }
}
var p = new Promise((resolve, reject) => {
let postbody = [];
req.on("data", chunk => {
postbody.push(chunk);
})
req.on('end', () => {
let postbodyBuffer = Buffer.concat(postbody);
resolve(postbodyBuffer)
})
});
p.then((postbodyBuffer) => {
let responsebody = []
var request = http.request(options, (response) => {
response.on('data', (chunk) => {
responsebody.push(chunk)
})
response.on("end", () => {
responsebodyBuffer = Buffer.concat(responsebody)
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.end(responsebodyBuffer);
})
})
request.write(postbodyBuffer)
request.end();
})
});
server.listen(3002, () => {
console.log("runnng3002");
})
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
const options = {
hostname: 'localhost',
port: 8080,
path: req.url,
method: req.method
};
const proxyReq = http.request(options, (proxyRes) => {
proxyRes.on('data', (chunk) => {
res.write(chunk);
});
proxyRes.on('end', () => {
res.end();
});
});
proxyReq.on('error', (e) => {
console.error(`请求遇到问题: ${e.message}`);
});
req.on('data', (chunk) => {
proxyReq.write(chunk);
});
req.on('end', () => {
proxyReq.setHeader('Content-Type', 'text/html;charset=utf-8');
proxyReq.end();
});
});
server.listen(3003, () => {
console.log('服务器正在监听3003端口');
});
const fs = require('fs');
const path = require('path');
let http = require('http');
let https = require('https');
const proxyoptions = {
key: fs.readFileSync(path.join(__dirname, './key.pem')),
cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};
const server = https.createServer(proxyoptions);
server.on("request", (req, res) => {
var { connection, host, ...originHeaders } = req.headers;
var options = {
"method": req.method,
"hostname": "localhost",
"port": "8080",
"path": req.url,
"headers": { originHeaders }
}
var p = new Promise((resolve, reject) => {
let postbody = [];
req.on("data", chunk => {
postbody.push(chunk);
})
req.on('end', () => {
let postbodyBuffer = Buffer.concat(postbody);
resolve(postbodyBuffer)
})
});
p.then((postbodyBuffer) => {
let responsebody = []
var request = http.request(options, (response) => {
response.on('data', (chunk) => {
responsebody.push(chunk)
})
response.on("end", () => {
responsebodyBuffer = Buffer.concat(responsebody)
res.setHeader('Content-Type', 'text/html;charset=utf-8');
res.end(responsebodyBuffer);
})
})
request.write(postbodyBuffer)
request.end();
})
});
server.listen(3004, () => {
console.log("runnng3004");
})
const fs = require('fs');
const path = require('path');
let http = require('http');
let https = require('https');
const proxyoptions = {
key: fs.readFileSync(path.join(__dirname, './key.pem')),
cert: fs.readFileSync(path.join(__dirname, './cert.pem')),
};
const server = https.createServer(proxyoptions, (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf8' });
const options = {
hostname: 'localhost',
port: 8080,
path: req.url,
method: req.method
};
const proxyReq = http.request(options, (proxyRes) => {
proxyRes.on('data', (chunk) => {
res.write(chunk);
});
proxyRes.on('end', () => {
res.end();
});
});
proxyReq.on('error', (e) => {
console.error(`请求遇到问题: ${e.message}`);
});
req.on('data', (chunk) => {
proxyReq.write(chunk);
});
req.on('end', () => {
proxyReq.setHeader('Content-Type', 'text/html;charset=utf-8');
proxyReq.end();
});
});
server.listen(3004, () => {
console.log('服务器正在监听3004端口');
});