当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript IncomingMessage.on方法代码示例

本文整理汇总了TypeScript中http.IncomingMessage.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript IncomingMessage.on方法的具体用法?TypeScript IncomingMessage.on怎么用?TypeScript IncomingMessage.on使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在http.IncomingMessage的用法示例。


在下文中一共展示了IncomingMessage.on方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: return

  return (req: IncomingMessage, res: ServerResponse) => {
    // return 404 if path is /bogus-route to pass the test, lambda doesn't have paths
    if (req.url.includes('/bogus-route')) {
      res.statusCode = 404;
      return res.end();
    }

    let body = '';
    req.on('data', chunk => (body += chunk));
    req.on('end', () => {
      const urlObject = url.parse(req.url, true);
      const event = {
        httpMethod: req.method,
        body: body,
        path: req.url,
        queryStringParameters: urlObject.query,
        requestContext: {
          path: urlObject.pathname,
        },
        headers: req.headers,
      };
      const callback = (error, result) => {
        if (error) throw error;
        res.statusCode = result.statusCode;
        for (let key in result.headers) {
          if (result.headers.hasOwnProperty(key)) {
            res.setHeader(key, result.headers[key]);
          }
        }
        res.write(result.body);
        res.end();
      };
      handler(event as any, {} as any, callback);
    });
  };
开发者ID:apollostack,项目名称:apollo-server,代码行数:35,代码来源:lambdaApollo.test.ts

示例2: callback

  }, (response: IncomingMessage) => {
    if (response.statusCode >= 400) {
      callback(new Error("Request error, status " + response.statusCode + ": " + response.statusMessage))
      return
    }

    const redirectUrl = response.headers.location
    if (redirectUrl != null) {
      if (redirectCount < maxRedirects) {
        doDownload(redirectUrl, destination, redirectCount++, callback)
      }
      else {
        callback(new Error("Too many redirects (> " + maxRedirects + ")"))
      }
      return
    }

    const downloadStream = createWriteStream(destination)
    response.pipe(downloadStream)
    downloadStream.on("finish", () => downloadStream.close(callback))

    let ended = false
    response.on("end", () => {
      ended = true
    })

    response.on("close", () => {
      if (!ended) {
        callback(new Error("Request aborted"))
      }
    })
  })
开发者ID:Dreadchild,项目名称:electron-builder,代码行数:32,代码来源:httpRequest.ts

示例3: return

  return (req: IncomingMessage, res: ServerResponse) => {
    // return 404 if path is /bogus-route to pass the test, azure doesn't have paths
    if (req.url.includes('/bogus-route')) {
      res.statusCode = 404;
      return res.end();
    }

    let body = '';
    req.on('data', chunk => (body += chunk));
    req.on('end', () => {
      const urlObject = url.parse(req.url, true);
      const request = {
        method: req.method,
        body: body && JSON.parse(body),
        path: req.url,
        query: urlObject.query,
        headers: req.headers,
      };
      const context = {
        done(error, result) {
          if (error) throw error;
          res.statusCode = result.status;
          for (let key in result.headers) {
            if (result.headers.hasOwnProperty(key)) {
              res.setHeader(key, result.headers[key]);
            }
          }
          res.write(result.body);
          res.end();
        },
      };
      handler(context as any, request as any);
    });
  };
开发者ID:apollostack,项目名称:apollo-server,代码行数:34,代码来源:azureFunctionApollo.test.ts

示例4: outputIfFinished

 http.get(url, (response: IncomingMessage) => {
     let buf: string = "";
     response.setEncoding("utf8");
     response.on("data", (result: string) => {
         buf += result;
     });
     response.on("end", () => {
         outputIfFinished(buf, index);
     });
 });
开发者ID:kyanro,项目名称:nodeschool-learnyounode,代码行数:10,代码来源:main.ts

示例5:

 let server: Server = http.createServer((req: IncomingMessage, res: ServerResponse) => {
     let upperBuf: string = "";
     req.setEncoding("utf8");
     req.on("data", (data: string) => {
         upperBuf += data.toUpperCase();
     });
     req.on("end", () => {
         res.end(upperBuf);
     });
 });
开发者ID:kyanro,项目名称:nodeschool-learnyounode,代码行数:10,代码来源:main.ts

示例6: _handler

	private _handler(request: IncomingMessage, response: ServerResponse) {
		if (request.method === 'GET') {
			if (/\.js(?:$|\?)/.test(request.url)) {
				this._handleFile(request, response, this.instrument);
			}
			else {
				this._handleFile(request, response);
			}
		}
		else if (request.method === 'HEAD') {
			this._handleFile(request, response, false, true);
		}
		else if (request.method === 'POST') {
			request.setEncoding('utf8');

			let data = '';
			request.on('data', function (chunk) {
				data += chunk;
			});

			request.on('end', () => {
				try {
					let rawMessages: any = JSON.parse(data);

					if (!Array.isArray(rawMessages)) {
						rawMessages = [rawMessages];
					}

					const messages: Message[] = rawMessages.map(function (messageString: string) {
						return JSON.parse(messageString);
					});

					Promise.all(messages.map(message => this._handleMessage(message))).then(
						() => {
							response.statusCode = 204;
							response.end();
						},
						() => {
							response.statusCode = 500;
							response.end();
						}
					);
				}
				catch (error) {
					response.statusCode = 500;
					response.end();
				}
			});
		}
		else {
			response.statusCode = 501;
			response.end();
		}
	}
开发者ID:bryanforbes,项目名称:intern,代码行数:54,代码来源:Server.ts

示例7: callback

 (res: IncomingMessage) => {
   const data: Buffer[] = [];
   res.on("data", chunk => data.push(chunk as Buffer));
   res.on("end", () => {
     const result = res as ArangojsResponse;
     result.request = req;
     result.body = Buffer.concat(data);
     if (called) return;
     called = true;
     callback(null, result);
   });
 }
开发者ID:arangodb,项目名称:arangojs,代码行数:12,代码来源:request.node.ts

示例8: Promise

  return new Promise((resolve, reject) => {
    let buf: Buffer = null;

    req.on("data", (chunk: Buffer) => {
      if (buf === null) {
        buf = chunk;
        return;
      }
      buf = Buffer.concat([buf, chunk]);
    });

    req.on("end", () => {
      resolve(buf);
    });
  });
开发者ID:mofax,项目名称:leafless,代码行数:15,代码来源:body.ts

示例9: Promise

    return new Promise((resolve) => {
        let fullBody = '';

        req.on('data', (chunk: any) => {
            fullBody += chunk.toString();
        });

        req.on('end', ()=> {
            try {
                fullBody = JSON.parse(fullBody);
                resolve(fullBody);
            } catch (error) {
                resolve({});
            }
        });
    });
开发者ID:sqram,项目名称:koa-async-body,代码行数:16,代码来源:index.ts


注:本文中的http.IncomingMessage.on方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。