NestJs 静态目录配置

作者: stormKid | 来源:发表于2019-03-11 17:27 被阅读35次

网上查看了很多文档,发现很多都是自己实现中间件来完成此功能,不仅浪费时间,而且增加了太多的代码量。实际上,nest已经帮助我们封装好了相关功能。

1、查找线索

由于官方文档没有做详细解释说明,那么我们可以从此框架底层入手:
我们知道,nestjs底层用的是express,那么express是通过什么来完成静态目录构建的:

serve-static

2、搜索源码

我们在项目搜索栏目中搜索“serve-static”会发现如下图:


yarn.lock.png

也就是说,当我们在使用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 {
        /**
         * Enable or disable setting Cache-Control response header, defaults to true. 
         * Disabling this will ignore the immutable and maxAge options.
         */
        cacheControl?: boolean;

        /**
         * Set how "dotfiles" are treated when encountered. A dotfile is a file or directory that begins with a dot (".").
         * Note this check is done on the path itself without checking if the path actually exists on the disk.
         * If root is specified, only the dotfiles above the root are checked (i.e. the root itself can be within a dotfile when when set to "deny").
         * The default value is 'ignore'.
         * 'allow' No special treatment for dotfiles
         * 'deny' Send a 403 for any request for a dotfile
         * 'ignore' Pretend like the dotfile does not exist and call next()
         */
        dotfiles?: string;

        /**
         * Enable or disable etag generation, defaults to true.
         */
        etag?: boolean;

        /**
         * Set file extension fallbacks. When set, if a file is not found, the given extensions will be added to the file name and search for.
         * The first that exists will be served. Example: ['html', 'htm'].
         * The default value is false.
         */
        extensions?: string[];

        /**
         * Let client errors fall-through as unhandled requests, otherwise forward a client error.
         * The default value is false.
         */
        fallthrough?: boolean;

        /**
         * Enable or disable the immutable directive in the Cache-Control response header.
         * If enabled, the maxAge option should also be specified to enable caching. The immutable directive will prevent supported clients from making conditional requests during the life of the maxAge option to check if the file has changed.
         */
        immutable?: boolean;

        /**
         * By default this module will send "index.html" files in response to a request on a directory.
         * To disable this set false or to supply a new index pass a string or an array in preferred order.
         */
        index?: boolean | string | string[];

        /**
         * Enable or disable Last-Modified header, defaults to true. Uses the file system's last modified value.
         */
        lastModified?: boolean;

        /**
         * Provide a max-age in milliseconds for http caching, defaults to 0. This can also be a string accepted by the ms module.
         */
        maxAge?: number | string;

        /**
         * Redirect to trailing "/" when the pathname is a dir. Defaults to true.
         */
        redirect?: boolean;

        /**
         * Function to set custom headers on response. Alterations to the headers need to occur synchronously.
         * The function is called as fn(res, path, stat), where the arguments are:
         * res the response object
         * path the file path that is being sent
         * stat the stat object of the file that is being sent
         */
        setHeaders?: (res: express.Response, path: string, stat: any) => any;
    }
    function serveStatic(root: string, options?: ServeStaticOptions): express.Handler;
}

export = serveStatic;

3、使用方式:

说明:源码中的注释说的很清楚用法,由于现阶段技术有限,博主将项目目录作为文件地址来简单的使用。

代码使用:只需要一句代码:
在 main.ts文件中:

    //...
    import * as serveStatic from 'serve-static';
    async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    //....
    // 使用serve-static
    // '/public' 是路由名称,即你访问的路径为:host/public
    // serveStatic 为 serve-static 导入的中间件,其中'../public' 为本项目相对于src目录的绝对地址
    app.use('/public', serveStatic(path.join(__dirname, '../public'), {
     maxAge: '1d',
     extensions: ['jpg', 'jpeg', 'png', 'gif'],
    }));
    //....
    await app.startAllMicroservicesAsync();
    await app.listen(9871);
}
bootstrap();

在项目根目录下创建public目录:

目录创建.png

4、测试效果:

首先使用nestjs自带的upload api来上传文件,这里不做过多说明,最终通过postman完成测试文件上传:

测试上传.png

再使用浏览器浏览:


浏览图片.gif

相关文章

  • NestJs 静态目录配置

    网上查看了很多文档,发现很多都是自己实现中间件来完成此功能,不仅浪费时间,而且增加了太多的代码量。实际上,nest...

  • Django03模板提取

    静态目录 配置静态目录 STATIC_URL配置的静态目录位于项目根目录下的static文件夹,用于保存项目公共的...

  • 附录1.相关配置

    配置MySQL,修改工程__init__.py文件 配置数据库 配置语言和时区 配置静态目录 配置上传文件目录 修...

  • VMware克隆系统静态IP配置(NAT方式)

    1)单机手动配置静态IP单机静态IP配置信息在/etc/sysconfig/network-scripts目录下。...

  • Nginx配置不同location

    两个静态网页配置在不同目录 有一个vue静态网页工程目录放在/home/ubuntu/website/dist,包...

  • vuecli 3.0 打包

    静态资源需要设置与index页面不是同级目录的例如加上动态项目目录order的打包配置 普通打包配置

  • nest.js 集成项目环境配置 env config

    通过env搭配@nestjs/config实现 环境配置。 安装依赖 文件配置 创建环境配置文件 config T...

  • Dango项目配置流程

    django项目配置流程 manage.py 创建静态文件存放目录 创建APP 项目目录结构 配置数据库 创建数据表

  • Web安全 之 防盗链

    本文环境基于Ubuntu1604 目录 站点A 站点B 静态资源 Nginx配置 测试 站点A 站点B 静态资源 ...

  • Mac 使用 Nginx 在本地部署静态网站

    安装 安装 Brew 安装 Nginx 启动 Nginx 重启 配置 默认静态页面 默认配置 默认日志目录 新增的...

网友评论

    本文标题:NestJs 静态目录配置

    本文链接:https://www.haomeiwen.com/subject/spbbpqtx.html