http.createServer(function (req, res) { var url = URL.parse(req.url); res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(fs.readFileSync(__dirname + url.pathname)); })
http.request(options, (res) => { console.info('STATUS: ' + res.statusCode); process.exitCode = (res.statusCode === 200) ? 0 : 1; process.exit(); })
const downloadFileFromURI = uri => { return new Promise((res, rej) => { http.get(uri, response => { response.setDefaultEncoding('base64'); let body = `data:${response.headers['content-type']};base64,`; response.on('data', data => body += data); response.on('end', () => res(body)); }).on('error', e => { rej(`Error downloading file from ${uri}: ${e.message}`); }); }); }
// Initializes the server and mounts it in the generated build directory. http.createServer((req, res) => { // If we are accessing the root, it should be redirected to the default language, // We shouldn't get a 404 page. if (req.url === '/') { req.url = `/${mainLocale}` } mount(req, res) }).listen(port, () => { console.log(`\x1B[32mServer running at http://localhost:${port}/${mainLocale}/\x1B[39m`) })
https.get(url, function(response) { if (response.statusCode !== 200) reject(new Error(`HTTP error ${response.statusCode}`)); response.pipe(file); file.on('finish', function() { // file.close(); resolve(); }); }).on('error', (error) => { reject(error); });
var proxy = function (request, response) { var req = http.request(request.url + (request.url.indexOf('?') == -1 ? '?' : '&') + 'proxy=' + request.socket.localPort); req.addListener('response', function (res) { res.addListener('data', function (chunk) { response.write(chunk, 'binary'); }); res.addListener('end', function () { response.end(); }); response.writeHead(res.statusCode, res.headers); }); request.addListener('data', function (chunk) { req.write(chunk, 'binary'); }); request.addListener('end', function () { req.end(); }); }
function getPipInstaller() { // install pip return new Promise((resolve, reject) => { const pipFilePath = path.join(__dirname, "get-pip.py"); if (fs.existsSync(pipFilePath)) return resolve(pipFilePath); const file = fs.createWriteStream(pipFilePath); const request = https.get(pipUrl, function(response) { response.pipe(file); file.on("finish", function() { file.close(() => { resolve(pipFilePath); }); }); file.on("error", function(err) { fs.unlink(pipFilePath); reject(err); }); }); }); }
https.request({ hostname: 'f.stdlib.com', port: 443, path: `/utils/reflect/${search}`, method: method, headers: headers }, (libres) => { let lbuffers = []; libres.on('data', chunk => lbuffers.push(chunk)); libres.on('end', () => { let lbuffer = Buffer.concat(lbuffers); let json = {}; try { json = JSON.parse(lbuffer.toString()); } catch (e) { return callback(new Error('Unexpected stdlib reflect response: ' + lbuffer.toString())); } callback(null, json); }); })
if ( res.statusCode < 200 || res.statusCode >= 300 ) { return reject( new Error( `Status Code: ${ res.statusCode }` ) ); res.on( 'data', ( chunk ) => { data.push( chunk ); } ); res.on( 'end', () => resolve( Buffer.concat( data ).toString() ) ); } );
http.createServer(function (req, res) { var url = URL.parse(req.url, true), uri = decodeURIComponent(url.pathname), postData = ''; if (paths[uri] !== undefined) { if (req.method === 'POST') { req.on('data', function (chunk) { postData += chunk.toString(); }); req.on('end', function () { if (!req.headers['content-type'] || req.headers['content-type'].indexOf('multipart') !== 0) postData = qs.parse(postData); paths[uri](url, req, res, postData); }); } else { paths[uri](url, req, res); } } else { res.writeHead(404); res.end(); } })
// Retrieve Google Signin Keys (with cache control) function getGoogleKeyByKeyId(keyId) { if (cache[keyId] && cache.expiresAt > new Date()) { return cache[keyId]; } return new Promise((resolve, reject) => { https.get(`https://www.googleapis.com/oauth2/v3/certs`, res => { let data = ''; res.on('data', chunk => { data += chunk.toString('utf8'); }); res.on('end', () => { const {keys} = JSON.parse(data); const pems = keys.reduce((pems, {n: modulus, e: exposant, kid}) => Object.assign(pems, {[kid]: rsaPublicKeyToPEM(modulus, exposant)}), {}); if (res.headers['cache-control']) { var expire = res.headers['cache-control'].match(/max-age=([0-9]+)/); if (expire) { cache = Object.assign({}, pems, {expiresAt: new Date((new Date()).getTime() + Number(expire[1]) * 1000)}); } } resolve(pems[keyId]); }); }).on('error', reject); }); }
function getPipInstaller() { // install pip return new Promise((resolve, reject) => { const pipFilePath = path.join(__dirname, "get-pip.py"); if (fs.existsSync(pipFilePath)) return resolve(pipFilePath); const file = fs.createWriteStream(pipFilePath); const request = https.get(pipUrl, function(response) { response.pipe(file); file.on("finish", function() { file.close(() => { resolve(pipFilePath); }); }); file.on("error", function(err) { fs.unlink(pipFilePath); reject(err); }); }); }); }