我正在尝试AuthGuard按照文档使用装饰器和通行证 JWT 策略。
文档中的所有内容都很好用。但是我现在想保护具有包含在 JWT 中的范围的路由。所以这是我的应用程序生成的基本 jwt 负载:
{
"user": {
"id": "20189c4f-1183-4216-8b48-333ddb825de8",
"username": "user.test@gmail.com"
},
"scope": [
"manage_server"
],
"iat": 1534766258,
"exp": 1534771258,
"iss": "15f2463d-8810-44f9-a908-801872ded159",
"sub": "20189c4f-1183-4216-8b48-333ddb825de8",
"jti": "078047bc-fc1f-4c35-8abe-72834f7bcc44"
}
Run Code Online (Sandbox Code Playgroud)
这是AuthGuard装饰器保护的基本受保护路由:
@Get('protected')
@UseGuards(AuthGuard('jwt'))
async protected(): Promise<string> {
return 'Hello Protected World';
}
Run Code Online (Sandbox Code Playgroud)
我想添加选项并将该路由的访问权限限制为具有manager_serverJWT 范围的人员。所以在阅读了一些AuthGuard代码之后,我认为我可以写出类似的东西:
@Get('protected')
@UseGuards(AuthGuard('jwt', {
scope: 'manage_server'
}))
async protected(): Promise<string> {
return 'Hello Protected World';
}
Run Code Online (Sandbox Code Playgroud)
但是,我在文档中看不到可以使用此选项的地方。
我认为向 the 的validate函数添加一个选项参数JWTStrategy可以解决问题,但事实并非如此。这是我的validate函数(包含在 …
我的目标是运行一个提供 Angular 前端的 nestjs 应用程序。我在项目的根目录下有一个 Angular cli 脚手架应用程序(使用 @angular/cli 最新的 v8 测试版构建)。在这个文件夹中,我使用 nestjs cli 搭建了一个新的 nestjs 应用程序(应用程序名称是服务器)。
然后我 cd 进入服务器并运行 npm run start:prod 并得到以下错误:
../node_modules/@types/jasmine/ts3.1/index.d.ts:15:1 - error TS6200: Definitions of the following identifiers conflict with those in another file: describe, fdescribe, xdescribe, it, fit, xit, beforeEach, afterEach, beforeAll, afterAll, expect, CustomEqualityTester,
CustomMatcherFactory, DEFAULT_TIMEOUT_INTERVAL
15 type ImplementationCallback = (() => Promise<any>) | ((done: DoneFn) => void);
~~~~
node_modules/@types/jest/index.d.ts:24:1
24 declare var beforeAll: jest.Lifecycle;
~~~~~~~
Conflicts are in this file.
../node_modules/@types/jasmine/ts3.1/index.d.ts:235:9 - …Run Code Online (Sandbox Code Playgroud) 我遵循了Nest JS Crash 教程,Youtube Link,我遵循了这个,但是当我在服务中导入接口时,它显示错误
Nest 无法解析 ItemsService (?) 的依赖项。请确保索引 [0] 处的参数在 AppModule 上下文中可用。
我克隆了教程中给出的存储库,它工作正常,但是当我将该存储库的 src 文件夹复制到我的项目时,它会引发错误。这是我的服务文件
import { Injectable } from '@nestjs/common';
import { Item } from './interfaces/item.interface';
import { Model } from 'mongoose';
import { ItemsModule } from './items.module'
import { InjectModel } from '@nestjs/mongoose';
@Injectable()
export class ItemsService {
constructor(@InjectModel('Item') private readonly itemModel: Model<Item>) {}
});
}
Run Code Online (Sandbox Code Playgroud)
当我评论构造函数行时,它工作正常,我认为问题出在这一行上,
从'猫鼬'导入{模型};
因为当我将鼠标悬停在这一行时,它显示找不到此模块的声明。我什至尝试复制工作代码的 package.json 文件进行测试,但仍然错误保持不变
我的模块项目包含控制器文件、服务文件、模块文件、dto 文件、接口文件、架构文件、
我正在使用 Nestjs 创建一个 API,这对它来说相当新。我创建了 4 个模块,所有 4 个模块似乎都工作正常。我创建了另一个模块名称steps,但我在其中访问的所有路由都返回404 not found。
我是新来的,最近加入了 ORM 我正在尝试的代码
第一个查询:我想使用这种方法,但不确定如何按列进行分组,然后在该列上按 desc 排序
const result = await this.jobViewsRepository.find({
relations: ["jobs"],
loadEagerRelations: true,
order: { id: "DESC" },
skip: offset,
take: limit,
}
);
Run Code Online (Sandbox Code Playgroud)
我正在尝试是否可以在上面的查询中使用它
第二个问题:它对我来说完美无缺,我正在寻找的结果
const res = await this.jobViewsRepository.createQueryBuilder('jobViews')
.addSelect("COUNT(jobViews.user_id) AS jobViews_total_count" )
.leftJoinAndSelect(Jobs, "jobs", "jobs.id = jobViews.job_id")
.where("jobs.user_id != :id", { id: user_id })
.groupBy("jobViews.job_id")**
.orderBy('jobViews_total_count', 'DESC')**
.limit(limit)
.offset(offset)
.getRawMany();
Run Code Online (Sandbox Code Playgroud)
如果有人能帮我解决这个问题,我将不胜感激
谢谢
我尝试运行 NestJs 测试,但出现以下错误
Nest can't resolve dependencies of the (?). Please make sure that the argument at index [0] is available in the RootTestModule context.
该项目使用 Mongoose 连接到 MongoDB
您可以通过运行此存储库中的代码来重现错误 https://github.com/kruyvanna/nestjs-test-error
先感谢您
我目前有一个使用 Nestjs 的项目,我想知道它与 Bun 1.0 的兼容性如何。我在互联网上没有找到明确的答案,我不想将我的项目转移到bun上,只是因为兼容性问题而陷入死胡同。
这是一个目前使用react和mongodb的小项目,但我计划使用bun中sqlite的内置支持。
我已经尝试在项目中使用bun,并且我可以安装我当前正在使用的模块,但我担心它能走多远。我没有时间测试所有内容,所以如果我能得到一些信息那就太好了。
我想提供/dist位于 Nest 项目外部文件夹中的静态 HTML 文件。index.html已成功加载但无法加载任何 JS 文件(404错误)。
我有一个 Node/Express.js 项目,它使用
app.use('/', express.static('../client/dist'))
Run Code Online (Sandbox Code Playgroud)
它工作得很好。
然而,在 Nest 项目中,
app.setBaseViewsDir(join(__dirname, '../../client/dist'))
Run Code Online (Sandbox Code Playgroud)
不做的伎俩。
在AppController我试过
import { Response } from 'express';
@Get()
get(@Res() res: Response) {
res.sendFile('index.html', {
root: '../client/dist',
});
}
Run Code Online (Sandbox Code Playgroud)
但没有运气。
如前所述,index.html已成功加载。所以问题不是走错路。问题也不是 src-paths 错误,index.html因为在 Express 项目中使用了完全相同的文件。
/dist
|-index.html
|-main.js
|-etc.
Run Code Online (Sandbox Code Playgroud)
在 index.html 中:
<script type="text/javascript" src="main.js"></script>
Run Code Online (Sandbox Code Playgroud)
当我将 dist 文件夹放入 Nest 项目(并调整路径)时,它也不起作用。
我现在使用 express 模块:
import * as express from 'express'; …Run Code Online (Sandbox Code Playgroud) 我的.envNestJs 项目的根目录中有一个文件,其中包含一些 env 变量。
奇怪的是,我能够读取服务文件中的变量,但不能读取模块文件中的变量。
所以在像这样的服务文件中users.service.ts,这有效:
saveAvatar() {
const path = process.env.AVATAR_PATH // returns value from .env
}
Run Code Online (Sandbox Code Playgroud)
但是,当访问模块文件中的路径时,如auth.module.ts,这将返回一个空值:
@Module({
imports: [
JwtModule.register({
secretOrPrivateKey: process.env.SECRET // process.env.SECRET returns an empty string
})
]
})
Run Code Online (Sandbox Code Playgroud)
为什么呢?如何.env在 NestJs 中可靠地访问文件中的环境变量?
NestJS 使用默认记录器实现。它将输出发送到控制台。
我可以知道,如何配置默认记录器以将输出发送到文件、数据库。
此外,
如果我想在 NestJS 中使用 Winston,如何使用/注入/扩展各种传输选项。
它不应该与 NestJS 紧密耦合,并且始终能够替换为其他一些记录器。
nestjs ×10
node.js ×4
javascript ×3
typescript ×2
angular ×1
bun ×1
dotenv ×1
express ×1
logging ×1
static-files ×1
typeorm ×1
winston ×1