Skip to content

介绍

NestJS 9x 的官方网址

NodejsNestJS

TIP

nestJs需要先进行全局安装

bash
$ npm i -g @nestjs/cli
$ nest new project-name

目录结构

bash
├─src
  app.module.ts # 定义模块,每个模块都需要在此处定义声明
  main.ts # 用户配置,定义端口之类

  ├─auth # 权限的文件夹

  ├─user
      user.controller.ts
      user.entity.ts
      user.module.ts
      user.service.ts

  └─utils
  index.ts

      ├─exceptionHandler
      biz-exception.filter.ts
      http-exception.filter.ts
      token-exception.filter.ts

      ├─interceptorHander
      post-status.interceptor.ts

      └─R
              R.ts

webpack接管nestjs编译

- webapck.config.js
- webpack.dev.js
- webpack.prod.js

package.json中修改配置

json
...
"scripts": {
  "build": "nest build --webpackPath ./webpack.prod.js",
  "start": "nest start",
  "start:dev": "nest start --watch --webpackPath ./webpack.dev.js",
  "start:debug": "nest start --debug --watch",
  "start:prod": "node dist/main",
  ...
}

webapck.config.js

copy-webpack-plugin模块用来复制跟目录下面的 shared 文件到 nestjs 目录下面

bash
pnpm add copy-webpack-plugin -D
webapck.config.js详细配置
js
const path = require("path")
const webpack = require("webpack")
const CopyPlugin = require("copy-webpack-plugin")
const sharedDirPath = path.resolve(__dirname, "../shared")
module.exports = {
  entry: "./src/main.ts",
  target: "node",
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: "ts-loader",
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
    alias: {
      "@shared": sharedDirPath
    }
  },
  mode: "production",
  devServer: {
    hotOnly: true
  },
  optimization: {
    concatenateModules: true
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new CopyPlugin({
      patterns: [
        {
          from: sharedDirPath,
          to: "shared"
        }
      ]
    })
  ],
  output: {
    path: path.join(__dirname, "dist"),
    filename: "main.js"
  }
}