欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NestJs 靜態(tài)目錄配置詳解

 更新時間:2019年03月12日 08:32:35   作者:stormKid  
這篇文章主要介紹了NestJs 靜態(tài)目錄配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

網上查看了很多文檔,發(fā)現(xiàn)很多都是自己實現(xiàn)中間件來完成此功能,不僅浪費時間,而且增加了太多的代碼量。實際上,nest已經幫助我們封裝好了相關功能。

1、查找線索

由于官方文檔沒有做詳細解釋說明,那么我們可以從此框架底層入手:

我們知道,nestjs底層用的是express,那么express是通過什么來完成靜態(tài)目錄構建的:

serve-static

2、搜索源碼

我們在項目搜索欄目中搜索“serve-static”會發(fā)現(xiàn)如下圖:

也就是說,當我們在使用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、使用方式:

說明:源碼中的注釋說的很清楚用法,由于現(xiàn)階段技術有限,博主將項目目錄作為文件地址來簡單的使用。

代碼使用:只需要一句代碼:

在 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();

在項目根目錄下創(chuàng)建public目錄:

目錄創(chuàng)建.png

4、測試效果:

首先使用nestjs自帶的upload api來上傳文件,這里不做過多說明,最終通過postman完成測試文件上傳:

測試上傳.png

再使用瀏覽器瀏覽:

瀏覽圖片.gif

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • JS實現(xiàn)FLASH幻燈片圖片切換效果的方法

    JS實現(xiàn)FLASH幻燈片圖片切換效果的方法

    這篇文章主要介紹了JS實現(xiàn)FLASH幻燈片圖片切換效果的方法,實例分析了javascript操作圖片實現(xiàn)Flash幻燈效果的技巧,需要的朋友可以參考下
    2015-03-03
  • javascript css紅色經典選項卡效果實現(xiàn)代碼

    javascript css紅色經典選項卡效果實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了javascript css紅色經典選項卡效果的實現(xiàn)代碼,需要的朋友可以參考下
    2016-05-05
  • 微信小程序偽類選擇器的使用詳解

    微信小程序偽類選擇器的使用詳解

    這篇文章主要介紹了微信小程序偽類選擇器的使用詳解,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • iframe的基本介紹與使用

    iframe的基本介紹與使用

    本文將給大家詳細介紹一下iframe的基本介紹與使用,iframe(內嵌框架)是 HTML 中一種用于將一個網頁嵌入到另一個網頁中的標簽,它可以在一個頁面中顯示來自其他頁面的內容,需要的朋友可以參考下
    2024-02-02
  • JS 調試中常見的報錯問題解決方法

    JS 調試中常見的報錯問題解決方法

    下面小編就為大家?guī)硪黄狫S 調試中常見的報錯問題解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 淺析JavaScript定時器setTimeout的時延問題

    淺析JavaScript定時器setTimeout的時延問題

    這篇文章主要為大家詳細介紹了JavaScript中定時器setTimeout有最小時延的相關知識,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-11-11
  • 小程序分享模塊超級詳解(推薦)

    小程序分享模塊超級詳解(推薦)

    這篇文章主要介紹了小程序分享模塊超級詳解(推薦),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • typescript配置alias的詳細步驟

    typescript配置alias的詳細步驟

    這篇文章主要介紹了typescript配置alias,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • javascript自然分類法算法實現(xiàn)代碼

    javascript自然分類法算法實現(xiàn)代碼

    這篇文章介紹了javascript自然分類法算法實現(xiàn)代碼,有需要的朋友可以參考一下
    2013-10-10
  • JavaScript編寫九九乘法表(兩種任選)

    JavaScript編寫九九乘法表(兩種任選)

    本文主要介紹了JavaScript編寫九九乘法表的兩種方法,具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論