πŸ“¦ nestjs / serve-static

Serve static websites (SPA's) using Nest framework (node.js) πŸ₯¦

β˜… 509 stars β‘‚ 64 forks πŸ‘ 509 watching βš–οΈ MIT License
javascriptnestjsnodenode-frameworknodejsserve-statictypescript
πŸ“₯ Clone https://github.com/nestjs/serve-static.git
HTTPS git clone https://github.com/nestjs/serve-static.git
SSH git clone git@github.com:nestjs/serve-static.git
CLI gh repo clone nestjs/serve-static
renovate[bot] renovate[bot] chore(deps): update dependency typescript-eslint to v8.53.1 (#1872) 4610951 13 hours ago πŸ“ History
πŸ“‚ master View all commits β†’
πŸ“ .circleci
πŸ“ .github
πŸ“ .husky
πŸ“ lib
πŸ“ tests
πŸ“„ .gitignore
πŸ“„ .npmignore
πŸ“„ .prettierrc
πŸ“„ .release-it.json
πŸ“„ CONTRIBUTING.md
πŸ“„ index.d.ts
πŸ“„ index.js
πŸ“„ index.ts
πŸ“„ LICENSE
πŸ“„ package.json
πŸ“„ README.md
πŸ“„ renovate.json
πŸ“„ tsconfig.json
πŸ“„ README.md

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads Discord Backers on Open Collective Sponsors on Open Collective

Description

@nestjs/serve-static package for Nest, useful to serve static content like Single Page Applications (SPA). However, if you are building MVC application or want to serve assets files (images, docs), use the useStaticAssets() method (read more here) instead.

Installation

$ npm i --save @nestjs/serve-static

Example

See full example here.

Usage

Simply import ServeStaticModule in your Nest application.

import { Module } from '@nestjs/common';
import { join } from 'path';
import { ServeStaticModule } from '@nestjs/serve-static';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client')
    })
  ]
})
export class ApplicationModule {}

API Spec

The forRoot() method takes an options object with a few useful properties.

PropertyTypeDescription
rootPathstringStatic files root directory. Default: "client"
serveRootstringRoot path under which static app will be served. Default: ""
renderPathstring / RegExpPath to render static app (concatenated with the serveRoot value). Default: \* (wildcard - all paths). Note: RegExp is not supported by the @nestjs/platform-fastify.
excludestring[]Paths to exclude when serving the static app. WARNING! Not supported by fastify. If you use fastify, you can exclude routes using regexp (set the renderPath to a regular expression) instead.
serveStaticOptionsObjectServe static options (static files)
useGlobalPrefixbooleanIf true, static app will be prefixed by the global prefix set through setGlobalPrefix(). Default: false https://docs.nestjs.com/faq/global-prefix

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.