本文整理汇总了TypeScript中http.IncomingMessage类的典型用法代码示例。如果您正苦于以下问题:TypeScript IncomingMessage类的具体用法?TypeScript IncomingMessage怎么用?TypeScript IncomingMessage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IncomingMessage类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: 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"))
}
})
})
示例2: 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);
});
};
示例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:
http.get(url, (response: IncomingMessage) => {
response.setEncoding("utf8");
response.on("data", (result: string) => {
result.split("\n").forEach((char: string) => {
console.log(char);
});
});
});
示例5: 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);
});
});
示例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:
}, (res: IncomingMessage) => {
res.setEncoding("utf8");
let data = "";
res.on("data", (chunk: string) => {
data += chunk;
}).on("end", () => {
console.log(data);
});
});
示例8: 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);
});
}