本文整理汇总了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);
});
};
示例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"))
}
})
})
示例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);
});
};
示例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);
});
});
示例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);
});
});
示例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();
}
}
示例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);
});
}
示例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);
});
});
示例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({});
}
});
});