本文整理汇总了TypeScript中http.ServerResponse.end方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ServerResponse.end方法的具体用法?TypeScript ServerResponse.end怎么用?TypeScript ServerResponse.end使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.ServerResponse
的用法示例。
在下文中一共展示了ServerResponse.end方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: Date
let server: Server = http.createServer((req: IncomingMessage, res: ServerResponse) => {
if (req.method !== "GET") {
return;
}
let parsedUrl: Url = url.parse(req.url, true);
if (parsedUrl.pathname.lastIndexOf("/api/parsetime", 0) === 0) {
let iso: string = parsedUrl.query.iso;
let date: Date = new Date(iso);
let responseDate: Object = {
"hour": date.getHours(),
"minute": date.getMinutes(),
"second": date.getSeconds(),
};
res.end(JSON.stringify(responseDate));
return;
}
if (parsedUrl.pathname.lastIndexOf("/api/unixtime", 0) === 0) {
let iso: string = parsedUrl.query.iso;
let date: Date = new Date(iso);
let responseDate: Object = {
"unixtime": date.getTime()
};
res.end(JSON.stringify(responseDate));
return;
}
res.end();
});
示例2: makeSubpath
form.parse(req, (err, fields:Fields, files:Files)=> {
if (!files) {
res.end(JSON.stringify({ok: false, error: "no_file_data"}));
}
var file = files["file"];
var tmpPath = file.path;
var subDir = makeSubpath(token);
var newDir = '/thumbs/' + subDir;
var newPath = newDir + "/" + jpgFile;
fs.exists(newDir, (exists:boolean)=> {
if (!exists) {
// console.log("Folder " + newDir + " not exist");
mkdirp(newDir, (err)=> {
if (err) {
res.end(JSON.stringify({ok: false, error: "cant_create_subdir"}));
console.error(err);
} else {
moveFile(tmpPath, newPath);
}
});
} else {
moveFile(tmpPath, newPath);
}
});
res.writeHead(200, {'content-type': 'text/plain'});
res.end(JSON.stringify({ok: true, url: subDir + "/" + jpgFile}));
});
示例3:
readstream.on("error", err => {
if ((err.code = "ENOENT")) {
// file not found
res.statusCode = 404;
res.end("Not Found");
} else {
console.error(err);
res.statusCode = 500;
res.end("Unknown Server Error");
}
});
示例4: createServer
const server = createServer((req: IncomingMessage, res: ServerResponse) => {
const credentials = auth(req);
if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic realm="example"');
res.end('Access denied');
} else {
res.end('Access granted');
}
});
示例5: validate
private validate(req: ServerRequest, resp: ServerResponse) {
let params = query.parse(url.parse(req.url).query)
if (params['hub.mode'] === 'subscribe' && params['hub.verify_token'] === this.options.verifyToken) {
resp.statusCode = 200
resp.end(params['hub.challenge'])
console.log('validation: ok')
} else {
resp.statusCode = 403
resp.end()
console.log('validation: fail')
}
}
示例6: _send404
private _send404(response: ServerResponse) {
response.writeHead(404, {
'Content-Type': 'text/html;charset=utf-8'
});
response.end(`<!DOCTYPE html><title>404 Not Found</title><h1>404 Not Found</h1>` +
`<!-- ${new Array(512).join('.')} -->`);
}
示例7: catch
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);
});
this.executor.log('Received HTTP messages');
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();
}
});
示例8: 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);
});
};
示例9: buf
ctx.buffer = async function buf(body: Buffer) {
if (Buffer.isBuffer(body)) {
res.end(body);
} else {
throw new Error(`response.buffer expects a Buffer found ${typeof body}`);
}
};
示例10:
req.on('end', () => {
//TODO: integrity check
resp.statusCode = 200
resp.end()
this.handlePayload(JSON.parse(data))
console.log('message receive: ok')
})