exports.handler = (event, context, callback) => { const APIurl = 'http://www.prh.noaa.gov/cphc/tcpages/archive/2018/TCPCP1.EP102018.049.201808121452'; http.get(APIurl, (res) => { console.log('API Call HTTP Code: ', res.statusCode); // this indicates if the HTTP request was valid var tempData = ""; res.on('data', (d) => { tempData += d; }); // this is the logic that gets executed once a successful API call is completed res.on('end', (d) => { console.log('completed request'); // now process data returned from the API call var returnData = tempData.toString('utf8'); //var returnData = eval('(' + tempData.toString('utf8') + ')'); //console.log(JSON.stringify(returnData)); //console.log(returnData.slice(0,100)); const bulletinStart = returnData.search("BULLETIN"); const yearStart = returnData.search(" 2018"); const timeStart = returnData.search("2018"); console.log("Time Start:" + timeStart); var forecastDate = returnData.slice(timeStart + 5, yearStart + 5); forecastDate = forecastDate.replace("00",":00"); forecastDate = forecastDate.replace("HST", "Hawaiian Standard Time,"); forecastDate = forecastDate.replace("MDT", "Mountain Daylight Time,"); forecastDate = forecastDate.replace("PDT", "Pacific Daylight Time,"); forecastDate = forecastDate.replace("Aug", "August"); forecastDate = forecastDate.replace("Mon", "Monday,"); forecastDate = forecastDate.replace("Fri", "Friday,"); forecastDate = forecastDate.replace("Sat", "Saturday,"); console.log("Forecast Date: " + forecastDate); const nextAdvisoryStart = returnData.search("NEXT ADVISORY"); var nextAdvisory = returnData.slice(nextAdvisoryStart + 54, nextAdvisoryStart + 65); nextAdvisory = nextAdvisory.replace("00",":00"); nextAdvisory = nextAdvisory.replace("MDT", "Mountain Daylight Time"); nextAdvisory = nextAdvisory.replace("PDT", "Pacific Daylight Time"); nextAdvisory = nextAdvisory.replace("HST", "Hawaiian Standard Time"); console.log("Next Advisory: " + nextAdvisory); const coordinatesStart = returnData.search("LOCATION"); const coordinates = returnData.slice(coordinatesStart + 11, coordinatesStart + 23); const northCoordinateEnd = coordinates.search("N"); const westCoordinateStart = coordinates.search("W"); const northCoordinate = coordinates.slice(0, northCoordinateEnd); const westCoordinate = coordinates.slice(westCoordinateStart - 5, westCoordinateStart); //console.log("Coordinates: " + coordinates); console.log("North Coordinate: " + northCoordinate + " North"); console.log("West Coordinate: " + westCoordinate + " West"); const locationStart = returnData.search("ABOUT"); const windsStart = returnData.search("MAXIMUM SUSTAINED WINDS"); const location = returnData.slice(locationStart + 6, windsStart - 1); const locationMilesEnd = location.search("MI"); const locationKiloEnd = location.search("KM"); const locationValue = location.slice(0, locationMilesEnd - 1); var locationGeography = location.slice(locationKiloEnd + 3, 99); locationGeography = locationGeography.replace("WSW OF","West Southwest of"); locationGeography = locationGeography.replace("W OF","West of"); locationGeography = locationGeography.replace("N OF","North of"); locationGeography = locationGeography.replace("SSE OF", "South Southeast of"); locationGeography = locationGeography.replace("ESE OF", "East Southeast of"); locationGeography = locationGeography.replace("SE OF", "Southeast of"); const secondLocation = locationGeography.search("ABOUT"); if (secondLocation > 1) { console.log("Removed second location details"); locationGeography = locationGeography.slice(0, secondLocation - 1); } console.log("Second Location: " + secondLocation); console.log("Location: " + locationValue + " miles " + locationGeography); //console.log("Geography: " + locationGeography); const winds = returnData.slice(windsStart + 26, windsStart + 33); const windsValueDigits = winds.search("MPH") - 1; const windsValue = winds.slice(0, windsValueDigits); console.log("Winds: " + windsValue + " MPH"); var stormType = "Tropical Storm"; if (windsValue < 39) { stormType = "Tropical Depression"; } else if (windsValue > 74) { stormType = "Hurricane"; } console.log("Storm Type: " + stormType); const movementStart = returnData.search("PRESENT MOVEMENT"); const movement = returnData.slice(movementStart + 19, movementStart + 45); const movementAT = movement.search("AT"); const movementMPH = movement.search("MPH"); const movementOR = movement.search("OR"); const movementSpeed = movement.slice(movementAT + 3, movementMPH - 1); var movementDirection = movement.slice(0, movementOR); movementDirection = movementDirection.replace("WNW ", "West Northwest"); movementDirection = movementDirection.replace("NNW ", "North Northwest"); movementDirection = movementDirection.replace("NW ", "Northwest"); movementDirection = movementDirection.replace("N ", "North"); movementDirection = movementDirection.replace("W ", "West"); //console.log("Movement: " + movement); console.log("Movement: " + movementDirection + " at " + movementSpeed + " MPH"); //console.log("Movement Speed: " + movementSpeed + " MPH"); const pressureStart = returnData.search("MINIMUM CENTRAL PRESSURE"); const pressure = returnData.slice(pressureStart + 27, pressureStart + 33); const pressureDigits = pressure.search("MB") - 1; const pressureValue = pressure.slice(0, pressureDigits); //console.log("Pressure Digits: " + pressureDigits); console.log("Pressure: " + pressureValue); const surfStart = returnData.search("SURF: "); const windStart = returnData.search("WIND: "); if (surfStart > 0) { var surfForecast = returnData.slice(surfStart + 6, windStart); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); surfForecast = surfForecast.replace("\n", " "); console.log("Surf Forecast: " + surfForecast); } var s3 = new aws.S3(); var getParams = {Bucket : 'hurricane-data', Key : 'currStorms.json'}; s3.getObject(getParams, function(err, data) { if(err) console.log('Error getting storm data : ' + err); else { var returnData = eval('(' + data.Body + ')'); // this is the storm number in the array const stormNumber = 0; // this is the detail unique to the storm var stormData = returnData[0].storms[stormNumber]; console.log('current storm data: ' + JSON.stringify(stormData)); stormData.peakWinds = Number(windsValue); stormData.pressure = Number(pressureValue); stormData.location.lat = northCoordinate + " North"; stormData.location.long = westCoordinate + " West"; stormData.location.proximity = locationValue + " miles " + locationGeography; stormData.location.name = "Hawaii"; stormData.location.distance = Number(locationValue); stormData.movement.direction = movementDirection; stormData.movement.speed = Number(movementSpeed); console.log('updated storm data: ' + JSON.stringify(stormData)); if (returnData[0].latestUpdate === forecastDate && returnData[0].storms[stormNumber] === stormData) { console.log("No Changes"); } else { console.log("Updated Forecast"); // overlay updated storm information returnData[0].latestUpdate = forecastDate; returnData[0].nextUpdate = nextAdvisory; returnData[0].storms[stormNumber] = stormData; var s3 = new aws.S3(); var postData = JSON.stringify(returnData); var putParams = {Bucket : 'hurricane-data', Key : 'currStorms.json', Body: postData}; // write to an S3 bucket s3.putObject(putParams, function(err, data) { if(err) console.log('Error posting data' + err); else console.log('Successfully posted data' + putParams.Body); }); } } }); }); }).on('error', (e) => { console.error(e); }); };
const { async_id_symbol } = process.binding('async_wrap'); const { newUid } = require('internal/async_hooks'); const agent = new http.Agent(); agent.createConnection = common.mustCall((cfg) => { const sock = new net.Socket(); // Fake the handle so we can enforce returning an immediate error sock._handle = { connect: common.mustCall((req, addr, port) => { return uv.UV_ENETUNREACH; }), readStart() {}, close() {} }; // Simulate just enough socket handle initialization sock[async_id_symbol] = newUid(); sock.connect(cfg); return sock; }); http.get({ host: '127.0.0.1', port: 1, agent }).on('error', common.mustCall((err) => { assert.strictEqual(err.code, 'ENETUNREACH'); }));
var http = require('http'); var url = process.argv[2]; function printOut (data){ console.log(data); } function callback (response){ response.setEncoding('utf8').on('data', printOut); }; http.get(url, callback);
var http=require('http'); http.get(process.argv[2],function(response){ response.setEncoding('utf8'); response.on('data', function(data){ // data.forEach(function(ele){ // process.stdout.write(ele + ); // }); process.stdout.write(data+'\n'); }); });
http.createServer(function(request, response) { var pathname = url.parse(request.url).pathname.substring(1); if (pathname === "" || pathname === "home") { response.writeHead(200, {"Content-Type": "text/html"}); response.write(fs.readFileSync('MarkerClusterer/dev files/map.html')); response.end(); } else if (pathname === "logo.gif") { response.writeHead(200, {"Content-Type": "image/gif"}); response.write(fs.readFileSync('logo.gif')); response.end(); } else if (pathname === "src/markerclusterer.js") { response.writeHead(200, {"Content-Type": "application/json"}); response.write(fs.readFileSync('MarkerClusterer/dev files/src/markerclusterer.js')); response.end(); } else if (pathname.substring(0,4) === "otx/") { pathname = pathname.substring(4); var thequery = url.parse(request.url, true).query; var startd = thequery.start; var endd = thequery.end; var searchterm = pathname; response.writeHead(200, {"Content-Type": "application/json"}); response.write("{ \"results\" : [\n"); var numpages = 1; makeAllRequests(numpages, pathname, response, searchterm, startd, endd); } else if (pathname.substring(0,7) === "gplace/") { console.log(pathname); pathname = pathname.substring(7); response.writeHead(200, {"Content-Type": "application/json"}); /*var options = { host: 'api.nytimes.com', path: '/svc/semantic/v2/geocodes/query.json?&name='+pathname+'&api-key=5a6fcc8f32060f3572a31023df9ae3e3:12:67526310' }*/ var options = { host: 'maps.googleapis.com', path: '/maps/api/geocode/json?address='+pathname+"&sensor=false" } http.get(options, function(res){ var data = ''; res.on('data', function (chunk){ data += chunk; }); res.on('error', function(chunk){ data += "error"; }); res.on('end',function(){ response.write(data); console.log("d: "+data); response.end(); }) }); } else if (pathname.substring(0,4) === "nyt/") { console.log(pathname); pathname = pathname.substring(4); response.writeHead(200, {"Content-Type": "application/json"}); var options = { host: 'api.nytimes.com', path: '/svc/search/v2/articlesearch.json?' + pathname } http.get(options, function(res){ var data = ''; res.on('data', function (chunk){ data += chunk; }); res.on('error', function(chunk){ data += "error"; }); res.on('end',function(){ response.write(data); response.end(); }) }); } else if (pathname.substring(0,4) === "myt/") { console.log(pathname); pathname = pathname.substring(4); var arr = pathname.split("/"); response.writeHead(200, {"Content-Type": "application/json"}); var cb = Array(arr[1]-arr[0]); for (var i = arr[0], j = 0; i < arr[1]; i++, j++) { var sDate = i+"0101"; var eDate = i+"1231"; cb[j] = { host: 'api.nytimes.com', path: '/svc/search/v2/articlesearch.json?' + "q="+arr[2]+"&begin_date="+sDate+"&end_date="+eDate+"&hl=true&api-key=columbiahack" } } response.write('{"years": ['); reck(0); function reck(k) { if (k==j) { response.write("]}"); response.end(); } else { http.get(cb[k], function(res){ var data = ''; res.on('data', function (chunk){ data += chunk; }); res.on('error', function(chunk){ data += "error"; }); res.on('end',function(){ response.write(data); if(k+1!=j){ response.write(', '); } reck(k+1); }); }); } } } }).listen(8888);
function downloadPhotos() { if(alreadyDownloading == true || downloadList.length < 1) return true; alreadyDownloading = true; var photo = downloadList.pop(); var localFile = pathPhotos+'/'+photo; var localPreview = pathPreviews+'/'+photo; console.log('['+photo+'] Downloading from http://'+cardAddr+cardPath+'/'+photo); var file = fs.createWriteStream(localFile); file.on('error', function(err) { console.log("FS: "+err); }); file.on('finish', function() { file.close(); alreadyDownloading = false; console.log('['+photo+'] Saved as '+localFile); gm(localFile).autoOrient().resize(previewWidth, previewHeight).write(localPreview, function (err) { if (!err) { console.log('['+photo+'] Resized to '+previewWidth+'x'+previewHeight); if(photos.length == 0 || photo != photos[photos.length-1].name) { photos.push({id: photoIndex, name: photo}); photoIndex++; io.emit('photo', { path: photo }); } }else{ console.log('Photo resize error: '+err); } }); if(downloadPrevious == true) { getPhotoList(); } if(downloadList.length > 0) { downloadPhotos(); }else{ console.log("All photos downloaded, waiting for new ones..."); } }); var options = { hostname: cardAddr, port: 80, path: cardPath+'/'+photo, method: 'GET' }; var request = http.get(options, function(response) { response.pipe(file); }); request.on('error', function(e) { console.log("HTTP Error: " + e.message); }); }
"use strict"; var http = require("http"), address = process.argv[2], bl = require("bl"); //var bl = new BufferList(); http.get(address, function (res) { //res.on("data", function (data) { // bl.append(data); //}); //res.on("end", function (data) { // console.log(bl.toString().length); // console.log(bl.toString()); //}); // you can use pipe funciton res.pipe(bl(function (err, data) { if (err) { return console.error(err); } data = data.toString(); console.log(data.length); console.log(data); })); });
var fs = require('fs'), http = require('http'); http.get({ host: 'shapeshed.com' }, function(res) { console.log("Got a response from shapeshed.com"); }).on('error', function(e) { console.log("There was an error from shapeshed.com"); }); fs.readFile('file1.txt', 'utf8', function (err, data) { if (err) { throw err; } console.log('File 1 read!'); }); http.get({ host: 'www.bbc.co.uk' }, function(res) { console.log("Got a response from bbc.co.uk"); }).on('error', function(e) { console.log("There was an error from bbc.co.uk"); }); fs.readFile('file2.txt', 'utf8', function (err, data) { if (err) { throw err; } console.log('File 2 read!'); });
var http = require('http'); var options = { host: 'shapeshed.com', port: 80, path: '/' }; http.get(options, function(res) { if (res.statusCode === 200) console.log('the site is up'); else console.log('The site is down!'); }).on('error', function(e) { console.log('There was an error: ' + e.message); });
/** * @file httpget.js * @brief * @author Airead Fan <*****@*****.**> * @date 2013/11/24 18:31:42 */ var http = require('http'); http.get({host: 'www.baidu.com'}, function (res) { res.setEncoding('utf8'); res.on('data', function (data) { console.log(data); }); });
/** * 官方文档7.9 * Created by Rain on 2017/4/28. */ const zlib = require('zlib'); const http = require('http'); const fs = require('fs'); const request = http.get({ host: 'localhost', path: '/', port: 1337, headers: {'Accept-Encoding': 'gzip,defalte'} //百度貌似只支持gzip }); request.on('response', (req) => { var output = fs.createWriteStream('./http-zlib-out.html'); //根据服务端返回的压缩方式 进行解压 switch (req.headers['content-encoding']) { case'gzip': req.pipe(zlib.createGunzip()).pipe(output); break; case 'deflate': req.pipe(zlib.createInflate()).pipe(output); break; default: req.pipe(output); break; } })
it('should return 200', done => { http.get('http://localhost:3001', res => { assert.equal(200, res.statusCode) done() }) })
var http = require('http'); http.get(process.argv[2],function(resp){ resp.setEncoding('utf8'); resp.on('data',console.log); resp.on('error',console.error); }).on('error',console.error);
fastify.listen(0, err => { t.error(err) fastify.server.unref() http.get('http://localhost:' + fastify.server.address().port) })
app.locals.bambuser_video=null; var TABLES={ desktop: { width:270, height:206 }, coffee: { width:1063, height:629 }} app.locals.table_size=TABLES[ZEN_TABLE_TYPE]; console.log("Using table type",ZEN_TABLE_TYPE,"size",app.locals.table_size) http.get({host:"api.bambuser.com", path: "/broadcast.json?username=neo4j&type=live&limit=3&api_key="+BAMBUSER_API_KEY}, function(res) { if (res.statusCode==200) { res.setEncoding('utf-8'); var content=""; res.on('data',function(data) { content += data.toString(); }) res.on('end',function() { var data=JSON.parse(content)['result']; if (data) { data .filter(function(vid) { return vid.title.match("[Zz]en") }) .sort(function(v1,v2) {return v2.created - v1.created }) .forEach(function(vid) { app.locals.bambuser_video=vid.vid; }); console.log(app.locals.bambuser_video) } }) } }).end(); // session id as key, array of commands as values, to replay and also for websocket push to all clients app.put("/session",routes.new_session); app.put('/session/:session', routes.make_active) app.post('/session/:session', routes.add_data) app.get('/session/:session', routes.get_data)
} catch(e) { dumpError(e); console.log("Failed To Load Ticket ticketEngine"); } finally { buffer_string = null; rdata = null; } }); }; console.log("STARTING UP TICKET SERVER...."); console.log("LOADING TICKET DATA...."); // fetch the data from the server and configure the ball engine var req = http.get(get_opts,responsefunc); // set timer to reload engine data every so often, incase IPs change in the database reload_timer = setInterval(function() { if (!loading) req = http.get(get_opts, responsefunc); }, RELOAD_INTERVAL); var cleaningTimer = setInterval(function() { console.log("going to run housekeeping"); if(ticketEngine) { ticketEngine.housekeeping(); console.log("hosekeeping"); } }, 90000); // every 90 seconds houskeeping ////////////////////////////////
function getDelayData(name) { var info = [], options = { host: 'www.tokyometro.jp', port: 80, path: '/unkou/history/' + name + '.html' }; console.log(options); http.get(options, function (res) { var body = ""; var lastModified = new Date(res.headers["last-modified"]); if (lastModified > lastRefleshed) { log.debug("***[" + name + "] is modified"); } else { log.debug("***[" + name + "] is updated"); return; } res.on('data', function (data) { body += data; }); res.on('end', function () { var document = jsdom(body, null, {features: { FetchExternalResources: false, ProcessExternalResources: false }}), table = document.getElementsByTagName("table")[0], events = table.getElementsByTagName("tr"); Train.find({name: name}, function (err, docs) { var train; if (docs.length === 0) { train = new Train(); train.name = name; } else { train = docs[0]; } var tinfo = {}; console.log(events.length); for (var i = 1; i < events.length; i ++) { var event = events[i], tds = event.getElementsByTagName("td"), time = tds[0], cause = tds[1], date = time.firstChild.textContent.match(/(\d+)月(\d+)日/), clock = time.childNodes[1].textContent.match(/(\d+)時(\d+)分/), isnormal = cause.textContent.indexOf("【") === -1, text = cause.childNodes[0].textContent, status = isnormal? "平常" : text.slice(1, text.length-1), match = cause.textContent.match(/(\d+)時(\d+)分/), date_obj = new Date(); if (isnormal && cause.textContent.indexOf("平常") === -1) { continue; } date_obj.setMonth(date[1]-1); date_obj.setDate(date[2]); date_obj.setHours(clock[1]); date_obj.setMinutes(clock[2]); log.debug("occured date : " + date_obj); if (date_obj < lastRefleshed) { break; } if (isnormal) { if (i != 1) { train.info.push(tinfo); train.save(function (err) { if (err) console.log("error while saving"); else console.log("save done!!"); }); info.push(tinfo); } tinfo = {}; tinfo["end"] = clock[1] + ":" + clock[2]; tinfo["end-date"] = date[1] + "/" + date[2]; continue; } else { if (i != 1) { tinfo["cause"] = status; console.log(status); tinfo["start"] = clock[1] + ":" + clock[2]; tinfo["start-date"] = date[1] + "/" + date[2]; } } } console.log(tinfo); if (tinfo.start) { console.log(tinfo); train.info.push(tinfo); info.push(tinfo); } train.save(function (err) { if (err) log.error(err); else console.log("save done!!"); }); }); }); }); }
reload_timer = setInterval(function() { if (!loading) req = http.get(get_opts, responsefunc); }, RELOAD_INTERVAL);
'callback': function(message, command, params) { var that=this; var verbose=false; var user=params[params.length-1]; for(var i=0;i<params.length;i++) { switch(params[i]) { case '-v': case '--verbose': verbose=true; break; } } http.get( { host: 'ws.audioscrobbler.com', path: '/2.0/?method=user.getrecenttracks&user='******'&api_key=a146a63252f43adef8fe055ca0113863&format=JSON&limit=1', port: 80}, function(response) { if(response.statusCode) { content=''; response.on('data', function(data) { content+=data.toString(); }); response.on('end', function() { try { content=JSON.parse(content); } catch(err) { that.logger.warn(err, 'Moka.lastfm'); that.logger.warn(content, 'Moka.lastfm.verbose'); return; } if(content.error) { if(verbose) { that.reply(content.message); } else { that.reply(message, 'Valami gond van, -v hogy tobbet megtudj!'); } } else { if(!content.recenttracks.track) { that.reply(message, 'ugy nez ki '+user+' meg nem hallgatott semmit :o'); return; } var track=content.recenttracks.track.length?content.recenttracks.track[0]:content.recenttracks.track; var artist=track.artist?track.artist['#text']:'Ismeretlen eloado'; var trackName=track.name?track.name:'Ismeretlen szam'; var album=track.album?track.album['#text']:false; var song=artist+' - '+trackName+(album?(' ('+album+')'):''); if(track['@attr']&&track['@attr'].nowplaying) { that.reply(message, params[1]+' epp ezt hallgatja: '+song); } else { if(track.date) { var date=track.date.uts; var diff=Math.floor(new Date().getTime()/1000)-date; var since= diff<60?'kevesebb, mint egy perce': diff<3600?(Math.floor(diff/60)+' perce'): diff<86400?(Math.floor(diff/3600)+' oraja'): diff<86400*30?(Math.floor(diff/86400)+' napja'): 'tobb mint egy honapja'; that.reply(message, user+' '+since+' ezt hallgatta: '+song); } else { that.reply(message, user+' ezt hallgatta valamikor: '+song); } } } }); } else { that.logger.warn('Unknown response', 'Moka.lastfm'); } }); }
/* Disabled net connect. Mock the different hostname:port. Result: Nock does not allow request to proceed. */ var log = require('./_log'); var events = ['socket', 'response', 'end', 'data', 'error']; var nock = require('../'); nock.disableNetConnect(); nock('http://someotherservice.com').get('/').reply(200, 'whaaa'); var http = require('http'); var req = http.get('http://www.google.com/'); req.once('error', function(err) { console.log(err.stack); }); events.forEach(log(req, 'req'));
t.equals(data.hasOwnProperty('statusCode'), true, 'Should have HTTP property statusCode;'); t.ok(isInteger(data.statusCode), 'statusCode is an integer'); t.equals(data.hasOwnProperty('requestHeader'), true, 'Should have HTTP property requestHeader;'); t.equals(data.hasOwnProperty('header'), true, 'Should have HTTP property header;'); t.equals(data.hasOwnProperty('contentType'), true, 'Should have HTTP property contentType;'); } function isInteger(n) { return isNumeric(n) && n % 1 == 0; } function isNumeric(n) { return !isNaN(parseFloat(n)) && isFinite(n); } // Request with a callback http.get(`http://localhost:${server.address().port}/`, function(res) {}); // Request without a callback http.get(`http://localhost:${server.address().port}/`); // Enable requests monitor.enable('requests'); monitor.disable('http'); // Request with a callback http.get(`http://localhost:${server.address().port}/`, function(res) {}); // Request without a callback http.get(`http://localhost:${server.address().port}/`);
var parser = require('./lib/parser'); var http = require('http'); var express = require('express'); var app = express(); var content = ''; http.get('http://axe-level-1.herokuapp.com/', function (res) { console.log('Get response', res.statusCode); res.on('data', function (chunk) { content += chunk; }); res.on('end', function () { // console.log(content); parser.run(content); }) }).on('error', function (err) { console.log(err); }); app.get('/api/result', function (req, res) { parser.getResult(function (result) { res.send(result); }); }); app.listen('5000');
var http = require('http') http.get(process.argv[2], function (response) { response.setEncoding('utf8') response.on('data', console.log) response.on('error', console.error) }).on('error', console.error)
server.listen(common.PORT, function() { http.get({port: common.PORT}, function(res) { server.close(); }); });
var data = http.get(options, function(res) { console.log("Got response: " + res.statusCode); var lastDig = (res.headers['content-type'].indexOf(';')); if(res.headers['content-type'] === "image/png") { name = ".png"; } else if(res.headers['content-type'].slice(0, lastDig) === "text/plain") { name = ".txt"; } else if (res.headers['content-type'].slice(0, lastDig) === "text/html") { name = ".html"; } else name = ".bin"; res.on("data", function(chunk) { fs.writeFile(options.path.slice(1)+name,""+chunk, function(err) { if(err) { console.log(err); } else { console.log("The file "+options.path.slice(1)+" was saved!") } }); }) }).on('error', function(e) { console.log("Got error: "+e.message);
setTimeout(()=>{ http.get(url, handleResponse) .on('error', handleError); }, delay);
return function(req, res, next) { var reqHost = req.headers.host.split(':')[0]; //远程请求的域名不能和访问域名一致,否则会陷入请求循环。 if(reqHost === param.host){ return; } var url = path.resolve(req.url.replace(/http:\/\/.+?\//,'/'));//兼容windows,windows平台下取得的req.url带http://部分 var prefix = url.indexOf(param.servlet + '?'); //不包含combo的servlet,认为是单一文件 if(prefix === -1){ //combo不处理html文件,但是需要接管其他资源 if(!fileReg.test(url)) { next(); return; } cosoleResp('Need', url); var filteredUrl = filterUrl(url); res.setHeader('Content-Type', mime.lookup(filteredUrl.split('?')[0])+';charset='+param.charset); var singleFileContent = readFromLocal(filteredUrl); if(singleFileContent){ res.end(singleFileContent); return; } var fileName = crypto.createHash('md5').update(reqHost+url).digest('hex'); var cachedFile = readFromCache(filteredUrl, fileName); if(cachedFile){ res.end(cachedFile); return; } //本地没有,从服务器获取 var requestOption = buildRequestOption(url, req); http.get(requestOption, function(resp) { var buffs = []; if(resp.statusCode !== 200){ cosoleResp('Not found', requestOption.host + requestOption.path + ' (host:'+ reset + yellow + ((requestOption && requestOption.host) ? requestOption.host : '') + reset + ')'); if (typeof next == "function") { delog.process(url+" [Transfer to NEXT]"); next(); return; } else { res.writeHead(404); res.end('File ' + requestOption.host + requestOption.path + ' not found.'); return; } } resp.on('data', function(chunk) { buffs.push(chunk); }); resp.on('end', function() { var buff = joinbuffers(buffs); //fix 80% situation bom problem.quick and dirty if(buff[0] === 239 && buff[1] === 187 && buff[2] === 191) { buff = buff.slice(3, buff.length); } if(isBinFile(filteredUrl)){ var binfileName = crypto.createHash('md5').update(reqHost+requestOption.path).digest('hex'); cacheFile(binfileName, buff); cosoleResp('Remote', requestOption.host + requestOption.path); res.end(buff); return; } cosoleResp('Remote', requestOption.host + requestOption.path); var charset = isUtf8(buff) ? 'utf8' : 'gbk'; var longestMatchPos = longgestMatchedDir(filteredUrl); //允许为某个url特别指定编码 var outputCharset = param.charset; if(longestMatchPos){ if(param.urlBasedCharset && param.urlBasedCharset[longestMatchPos]){ outputCharset = param.urlBasedCharset[longestMatchPos]; } } var singleFileContent = adaptCharset(buff, outputCharset, charset); var fileName = crypto.createHash('md5').update(reqHost+url).digest('hex'); cacheFile(fileName, buff, charset); res.end(singleFileContent); return; }); }).on('error',function(e){ debugInfo('Networking error:' + e.message); res.writeHead(404, { 'Content-Type': 'text/html;charset=utf-8'}); res.end('404 Error, File not found.'); return; }); return; } cosoleResp('Need', url); prefix = url.substring(0, prefix); var files = url.substring(prefix.length + param.servlet.length + 1, url.length); files = files.split(param.seperator, 1000); var reqArray = []; var prevNeedHttp = false ;//为循环做准备,用来判定上次循环的file是否需要通过http获取 var needHttpGet = ''; for(var i = 0, len = files.length; i < len; i++){ var file = files[i]; //combo URL有时候会多一个逗号 if(file === "") continue; var fullPath = filterUrl(prefix + files[i]); if(i === 0 ){ res.setHeader('Content-Type', mime.lookup(fullPath.split('?')[0])+';charset='+param.charset); } var fileContent = readFromLocal(fullPath); if(!fileContent){ if(prevNeedHttp){ needHttpGet += ',' + file; continue; } prevNeedHttp = true; needHttpGet = file; continue; } if(prevNeedHttp){ reqArray.push({file: needHttpGet, ready: false}); } prevNeedHttp = false; reqArray.push({file: file, content: fileContent, ready: true}); } if(prevNeedHttp){ reqArray.push({file: needHttpGet, ready:false}); } var reqPath = prefix + param.servlet + '?'; for(var i = 0, len = reqArray.length; i < len; i++){ if(reqArray[i].ready){ continue; } var cacheName = crypto.createHash('md5').update(reqHost+reqArray[i].file).digest('hex'); var cachedContent = readFromCache(reqArray[i].file, '/' + cacheName); if(cachedContent){ reqArray[i].content = cachedContent; reqArray[i].ready = true; continue; } (function(id) { var requestPath = reqPath + reqArray[id].file; var requestOption = buildRequestOption(requestPath, req); http.get(requestOption, function(resp) { if(resp.statusCode !== 200){ cosoleResp('Not found', requestOption.host + reqPath + reqArray[id].file + '('+ yellow +'host:'+ ((requestOption && requestOption.host) ? requestOption.host : '') + reset +')'); reqArray[id].ready = true; reqArray[id].content = 'File '+ reqArray[id].file +' not found.'; sendData(); return; } var buffs = []; resp.on('data', function(chunk) { buffs.push(chunk); }); resp.on('end', function() { cosoleResp('Remote', requestOption.host + reqPath + reqArray[id].file); reqArray[id].ready = true; var buff = joinbuffers(buffs); //fix 80% situation bom problem.quick and dirty if(buff[0] === 239 && buff[1] === 187 && buff[2] === 191) { buff = buff.slice(3, buff.length); } var fileName = crypto.createHash('md5').update(reqHost+reqArray[id].file).digest('hex'); var charset = isUtf8(buff) ? 'utf8' : 'gbk'; reqArray[id].content = adaptCharset(buff, param.charset, charset); cacheFile('/' + fileName, buff, charset); sendData(); }); }).on('error',function(e){ reqArray[id].ready = true; debug('Networking error:' + e.message); }); })(i); } var sendData = function(){ for(var j = 0, len = reqArray.length; j < len; j++){ if(reqArray[j].ready === false){ return; } } reqArray.forEach(function(reqNode){ res.write(reqNode.content); }); res.end(); } //如果全部都在本地可以获取到,就立即返回内容给客户端 sendData(); }
#!/bin/env node var http = require('http'); var url = process.argv[2]; http.get(url, function getResponse (response) { response.setEncoding('utf8'); response.on('data', console.log); response.on('error', console.error); });
it('should return a 200 response', function(done) { http.get({path: '/api/v1/tasks', port: 3001}, function(res) { assert.equal(res.statusCode, 200, 'Expected: 200 Actual: ' + res.statusCode); done(); }); });
var cheerio = require('cheerio') , http = require('http'); http.get('http://www.w3.org/html/wg/drafts/html/master/syntax.html', function (res) { var str = ''; res.setEncoding('utf8'); res.on('data', function (buf) { str += buf; }).on('end', function () { var $ = cheerio.load(str); var codes = $('dfn#void-elements') .parent() .next() .text() .replace(/\s/gm,'') .split(",") .reduce(function (obj, code) { obj[code] = true; return obj; }, {}); console.log('/**'); console.log(' * This file automatically generated from `pre-publish.js`.'); console.log(' * Do not manually edit.'); console.log(' */'); console.log(); console.log('module.exports = %s;', JSON.stringify(codes, null, 2)); }); });