本文整理匯總了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');
};