本文整理汇总了TypeScript中express.Response.type方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Response.type方法的具体用法?TypeScript Response.type怎么用?TypeScript Response.type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类express.Response
的用法示例。
在下文中一共展示了Response.type方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: sendFile
export function sendFile(path: string, filename: string, response: Response) {
//Send a file as response
let options = {
dotfiles: "deny",
headers: {
"x-timestamp": Date.now(),
"x-sent": true
}
};
let extension = filename.split(".").pop();
switch(extension) {
case "json":
response.type("application/json");
break;
case "properties":
response.type("text/x-java-properties");
break;
case "js":
response.type("text/javascript");
break;
case "css":
response.type("text/css");
break;
}
response.sendFile(
path + "/" + filename,
options,
function(err: Error | any) {
if (err && response.statusCode !== HttpStatus.NOT_MODIFIED && err.code !== "ECONNABORT") {
if (response.statusCode === HttpStatus.NOT_FOUND && filename !== placeholder) {
sendFile(path, placeholder, response);
} else {
// Do not log:
// - Requests aborted
// - 404 on minified script
//TODO is this still necessary?
if(err.message !== "Request aborted"
&& filename.match(/l4w\-.*\.min\.js/) === null) {
console.log("utils.sendFile - " + err);
console.error("error msg:" + err.message);
console.error("error name:" + err.name);
}
if(err.status !== undefined) {
response.status(err.status).send("");
} else {
response.status(HttpStatus.NO_CONTENT).send("");
}
}
}
}
);
};
示例2: function
var serverError = function (response: Response, message: string, user?: User) {
logger.error(message, user);
response.type('application/json; charset=utf-8');
response.writeHead(500);
response.write('{"error": "' + message + '"}');
response.end();
};
示例3:
api.get(async (req: Request, res: Response) => {
let stream: Stream = await bimModel
.find({})
.lean(true)
.stream({ transform: JSON.stringify });
res.type("application/json");
stream.on("data", (doc: string) => {
// res.write();
entityIO.emit("entity", doc);
}).on("error", (err: Error) => {
console.log(err);
}).on("close", () => {
res.end(JSON.stringify({completed: true}));
});
});
示例4:
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
res.type("text/plain");
res.status(500);
res.send("500 - Server Error");
});
示例5:
(data: Dataset) => {
res.type(outputType);
res.send(outputFn(Dataset.fromJS(data.toJS())));
},
示例6: Date
const pngResponse = (response: Response) => {
response.setHeader('Expires', new Date(Date.now() + 604800000).toUTCString());
return response.type('image/png');
};