Skip to content

Instantly share code, notes, and snippets.

@LightSpeedC
Last active December 14, 2018 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LightSpeedC/6376018 to your computer and use it in GitHub Desktop.
Save LightSpeedC/6376018 to your computer and use it in GitHub Desktop.
Node.jsによる必要最小限のhttpサーバとhttpsサーバとhttp proxyサーバ ref: https://qiita.com/LightSpeedC/items/3cae342fd7e79a21869f
const http = require('http'), url = require('url');
http.createServer((cliReq, cliRes) => {
const x = url.parse(cliReq.url);
const opt = {host: x.hostname, port: x.port || 80, path: x.path,
method: cliReq.method, headers: cliReq.headers};
const svrReq = http.request(opt, svrRes => {
cliRes.writeHead(svrRes.statusCode, svrRes.headers);
svrRes.pipe(cliRes); });
cliReq.pipe(svrReq);
}).listen(8080);
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, () => console.log('Server http://localhost:3000'));
const https = require('https'), fs = require('fs');
const options = {pfx: fs.readFileSync('secure.pfx'),
passphrase: 'passphrase'};
https.createServer(options, (req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, () => console.log('Server https://localhost:3000'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment