nest.js创建以及error相关问题

开始之前,你可以使用 Nest CLI 创建项目,也可以克隆一个 starter project(两者的结果是一样的)。

若要使用 Nest CLI 构建项目,请运行以下命令。这将创建一个新的项目目录,并使用核心的 Nest 文件和支撑模块填充该目录,从而为项目创建一个传统的基本结构。建议初学者使用 Nest CLI 创建新项目。我们将 第一步 章节中继续采用这种方法。

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

选择要创建的目录:

例如你要创建的目录是D盘,那就是D:

创建一个目录 例如nest

D:\>cd nest

nest new nestdemo1

开始创建

如果创建失败!报错

Error: EPERM: operation not permitted, mkdir 'D:\nest\nestdemo1'

Failed to execute command: node @nestjs/schematics:application --name=nestdemo1 --directory=undefined --no-dry-run --no-skip-git --no-strict --package-manager=undefined --collection="@nestjs/schematics" --language="ts"

请下载npm i -g @nestjs/schematics

就会发现下载成功

npm WARN @nestjs/schematics@9.0.3 requires a peer of typescript@^4.3.5 but none is installed. You must install peer dependencies yourself.

+ @nestjs/schematics@9.0.3
added 53 packages from 84 contributors in 21.302s

如果继续创建显示

Error: EPERM: operation not permitted, mkdir 'D:\nest\demo1'

Failed to execute command: node @nestjs/schematics:application --name=demo1 --directory=undefined --no-dry-run --no-skip-git --no-strict --package-manager=undefined --collection="@nestjs/schematics" --language="ts"

那就是管理员控制台原因

直接再windows用管理员权限打开控制台进行创建

D:\>cd nest

D:\nest>nest new demo1
⚡  We will scaffold your app in a few seconds..

CREATE demo1/.eslintrc.js (665 bytes)
CREATE demo1/.prettierrc (51 bytes)
CREATE demo1/nest-cli.json (118 bytes)
CREATE demo1/package.json (1990 bytes)
CREATE demo1/README.md (3340 bytes)
CREATE demo1/tsconfig.build.json (97 bytes)
CREATE demo1/tsconfig.json (546 bytes)
CREATE demo1/src/app.controller.spec.ts (617 bytes)
CREATE demo1/src/app.controller.ts (274 bytes)
CREATE demo1/src/app.module.ts (249 bytes)
CREATE demo1/src/app.service.ts (142 bytes)
CREATE demo1/src/main.ts (208 bytes)
CREATE demo1/test/app.e2e-spec.ts (630 bytes)
CREATE demo1/test/jest-e2e.json (183 bytes)

? Which package manager would you ❤️  to use? (Use arrow keys)
> npm
  yarn
  pnpm

完事

让你使用一个创建的命令,懂得都懂了吧

 

NestJS是一个为TypeScript编写的开源、基于Node.js的Web框架,设计目标是提供更好的组织和模块化,使构建高效、可维护的服务器端应用程序变得更加简单。当在NestJS项目中使用axios库时,你可以轻松地进行HTTP请求,无论是发送GET、POST还是其他类型的API调用。 1. 首先,你需要安装axios作为依赖项。在你的项目目录下打开终端或命令提示符,运行: ```bash npm install axios @nestjs/common # 或者使用yarn yarn add axios @nestjs/common ``` 2. 在`@nestjs/core`模块中,axios通常是自动注入的,所以在服务(service)或控制器(controller)里可以直接使用它。例如,在一个控制器中: ```typescript import { Controller, Get, Post } from '@nestjs/common'; import { HttpService } from '@nestjs/common/services/http.service'; @Controller('api') export class MyController { constructor(private readonly http: HttpService) {} // 注入axios @Get() async getItems() { return this.http.get('https://jsonplaceholder.typicode.com/posts'); // 发送GET请求 } @Post() async postData(data: any) { return this.http.post('https://httpbin.org/post', data); // 发送POST请求 } } ``` 3. 如果你想自定义axios配置,可以在创建`HttpService`实例时传入配置选项,如设置超时时间或全局默认头: ```typescript constructor(private http: HttpService = new HttpService({ timeout: 1000 * 60 })) {} ``` 4. 当处理错误时,axios会抛出`AxiosError`,你可以捕获并处理这些异常: ```typescript try { const response = await this.http.get('...'); } catch (error) { console.error(error); } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼程序员

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值