NestJs 静态目录配置详解静态目录配置详解
网上查看了很多文档,发现很多都是自己实现中间件来完成此功能,不仅浪费时间,而且增加了太多的代码量。实际上,nest
已经帮助我们封装好了相关功能。
1、查找线索、查找线索
由于官方文档没有做详细解释说明,那么我们可以从此框架底层入手:
我们知道,nestjs底层用的是express,那么express是通过什么来完成静态目录构建的:
serve-static
2、搜索源码、搜索源码
我们在项目搜索栏目中搜索“serve-static”会发现如下图:
也就是说,当我们在使用nest框架的时候,serve-static 会随之而构建好,那么我们直接参考其源码即可:依赖地址依赖地址
Nestjs 源码:
// Type definitions for serve-static 1.13
// Project: https://github.com/expressjs/serve-static
// Definitions by: Uros Smolnik <https://github.com/urossmolnik>
// Linus Unnebäck <https://github.com/LinusU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
/* =================== USAGE ===================
import * as serveStatic from "serve-static";
app.use(serveStatic("public/ftp", {"index": ["default.html", "default.htm"]}))
=============================================== */
/// <reference types="express-serve-static-core" />
import * as express from "express-serve-static-core";
import * as m from "mime";
/**
* Create a new middleware function to serve files from within a given root directory.
* The file to serve will be determined by combining req.url with the provided root directory.
* When a file is not found, instead of sending a 404 response, this module will instead call next() to move on to the next
middleware, allowing for stacking and fall-backs.
*/
declare function serveStatic(root: string, options?: serveStatic.ServeStaticOptions): express.Handler;
declare namespace serveStatic {
var mime: typeof m;
interface ServeStaticOptions {
评论0
最新资源