node.js - Http Request in TypeScript -


i trying convert following snippet in nodejs typescript: how make http request in nodejs

here typescript code:

import * http 'http';  export class httprequest{ url: string; private path: string; private host: string; private args: array<array<string>>;  constructor(url: string, args?: string){     this.url = url;     this.processurl(this.url);     if(!!args){         this.processargs(args);     }     this.args = []; } private processurl(url: string): void {     let tld: number = url.lastindexof('.')     let sep: number = url.indexof('/', tld);     this.host = url.slice(0, sep);     this.path = url.slice(sep+1); } private processargs(args: string): void {     let sep: number = args.indexof('&');     if(sep < 0){         return ;     }     let argpair: string = args.slice(0, sep);     let apsep: number = argpair.indexof('=');     let k: string = argpair.slice(0, apsep);     let v: string = argpair.slice(apsep+1);     this.args.push([k,v]);     this.processargs(args.slice(sep+1)); } private preparepath(): string {     let path: string = `?`;     this.args.foreach((arg: array<string>, i: number): void => {         let k: string = arg[0];         let v: string = arg[1];         path += k + '=' + v;         if(i == this.args.length-1){             return ;         }         path += '&';     });     return path; } public addarg(key: string, value: string): void {     try{         this.args.push([key,value]);     } catch(err) {         console.log(err);     } } public addargs(args: array<array<string>>): void {     args.foreach((arg: array<string>): void => {         this.args.push(arg);     }); } public get(cb: (res: any) => any): void {     let opts = {         'host': this.host,         'path': `/${this.path}/${this.preparepath()}`     };     http.request(opts, (r: http.incomingmessage): void => {         let data = '';         r.on('data', (chunk: string): void => {             console.log('got chunk: ' + chunk);             data += chunk;         });         r.on('end', (): void =>{             console.log('response has ended');             console.log(data);             cb(data);         });         r.on('error', (err): void => {             console.log('following error occured during request:\n');             console.log(err);         })     }).end(); } public test(): void {     console.log(this.preparepath());     console.log(`/${this.path}/${this.preparepath()}`); } } 

here test code:

// test httprequest  import { httprequest } './httprequest';  const request = new httprequest('www.random.org/integers'); request.addarg('num', '1'); request.addarg('min', '1'); request.addarg('max', '50'); request.addarg('col', '1'); request.addarg('base', '10'); request.addarg('format', 'plain'); request.addarg('rnd', 'new'); request.test(); request.get((res: string): void => {     console.log('response received: ' + res); }); 

if works correctly (i checked link on firefox and, returns plain text random number) should number plain text. however, when console.log() response, nothing. doing wrong here?

try :

var request = require('request'); request('www.random.org/integers, function (error, response, body) {             console.log(response.statuscode);             cb(response.statuscode);            }); 

thanks!!


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -