New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

note-server

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

note-server

node server

latest
npmnpm
Version
1.0.0
Version published
Weekly downloads
2
100%
Maintainers
1
Weekly downloads
 
Created
Source

body-parser

上面的 post 请求参数是放在 url 上的,实际场景通常都在 body 上,而 express 直接通过 req.body 只能获取到 undefined,因此需要 body-parser 第三方库

middleware

中间件监听必须在路由注册之前

数据库 ORM Sequelize

sequelizeAuto Git

Sequelize-Auto 自动生成 Sequelize 模型

sequelizeAuto Git

项目设置别名

1、tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  }
}

在这个例子中,@controllers 和 @models 是你设置的别名,src/controllers 和 src/models 是别名对应的实际路径。* 是一个通配符,表示任何子目录或文件。

然后你就可以在你的代码中使用这个别名来导入模块了

2、nodejs 设置别名

然而,TypeScript 编译器能够理解这个别名,但 Node.js 运行时并不能。因此,你需要使用一个模块别名解析器,如 module-alias。

1、首先,安装 module-alias:

npm install module-alias

然后,在你的 package.json 文件中添加 _moduleAliases:

{
  "_moduleAliases": {
    "@": "./src"
  }
}

3、node 注册

最后,在你的应用的入口文件(通常是 app.ts 或 index.ts)的最顶部添加以下代码:

import 'module-alias/register';

Token 验证

jwt

1、jwt 生成 token 2、jwt 验证 token

Token 中间件

  • 1、白名单路由,直接放行
  • 2、token 验证失败,返回 401
  • 3、token 验证通过,将用户信息挂载到 req.user 上,放行

查询

1、查询排除某些字段

例如,查询用户信息,但是不希望返回密码,可以这样写:

let data = await model.findOne({
  where: { id },
  attributes: { exclude: ['password'] },
});

2、关联查询

查询关联表的字段

查询用户,并且查询用户关联的笔记类型集合

2.1 设置主外键关联

//此处为用户关联笔记类型为一对多,因此需要设置外键关联,关联的外键设置在 noteType 表中
// 当需要通过User查询笔记类型时,需要设置此外键关联关系
models.user.hasMany(models.noteType, { foreignKey: 'userId' });
// 此处为笔记类型中的userId字段 ,属于user表
// 如果不需要通过笔记类型查询用户,则不需要设置如下关联关系,如若需要通过noteType查询User,则需要设置
models.noteType.belongsTo(models.user, { foreignKey: 'userId' });
let data = await model.findOne({
  where: { id },
  attributes: { exclude: ['password'] },
  include: {
    model: modules.noteType,
  },
});

注册 Controller 和 Request 修饰器

请求过滤器统一处理异常和返回

FAQs

Package last updated on 07 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts