# note-server

> node server

Latest version **1.0.0** (published 2024-03-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install note-server
pnpm add note-server
yarn add note-server
bun add note-server
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2024-03-07 |
| First published | 2024-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 301.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | cjSOund |
| Maintainers | dlsound |

## Links

- npm: https://www.npmjs.com/package/note-server
- npm.io page: https://npm.io/package/note-server

## Dependencies (11)

- [mysql](https://npm.io/package/mysql.md) ^2.18.1
- [mysql2](https://npm.io/package/mysql2.md) ^3.9.2
- [express](https://npm.io/package/express.md) ^4.18.3
- [nodemon](https://npm.io/package/nodemon.md) ^3.1.0
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.2.0
- [sequelize](https://npm.io/package/sequelize.md) ^6.37.1
- [typescript](https://npm.io/package/typescript.md) ^5.3.3
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^9.0.2
- [module-alias](https://npm.io/package/module-alias.md) ^2.2.3
- [sequelize-auto](https://npm.io/package/sequelize-auto.md) ^0.8.8
- [express-generator](https://npm.io/package/express-generator.md) ^4.16.1

## Recent versions

- 1.0.0 (latest) — 2024-03-07

## README

# body-parser

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

# middleware

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

# 数据库 ORM Sequelize

[sequelizeAuto Git](https://github.com/sequelize/sequelize)

# Sequelize-Auto 自动生成 Sequelize 模型

[sequelizeAuto Git](https://github.com/sequelize/sequelize-auto)

# 项目设置别名

## 1、tsconfig.json

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

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

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

## 2、nodejs 设置别名

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

1、首先，安装 module-alias：

```bash
npm install module-alias
```

然后，在你的 package.json 文件中添加 \_moduleAliases：

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

### 3、node 注册

最后，在你的应用的入口文件（通常是 app.ts 或 index.ts）的最顶部添加以下代码：

```ts
import 'module-alias/register';
```

# Token 验证

## jwt

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

## Token 中间件

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

# 查询

## 1、查询排除某些字段

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

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

## 2、关联查询

查询关联表的字段

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

### 2.1 设置主外键关联

```ts
//此处为用户关联笔记类型为一对多，因此需要设置外键关联，关联的外键设置在 noteType 表中
// 当需要通过User查询笔记类型时，需要设置此外键关联关系
models.user.hasMany(models.noteType, { foreignKey: 'userId' });
// 此处为笔记类型中的userId字段 ，属于user表
// 如果不需要通过笔记类型查询用户，则不需要设置如下关联关系，如若需要通过noteType查询User，则需要设置
models.noteType.belongsTo(models.user, { foreignKey: 'userId' });
```

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

# 注册 Controller 和 Request 修饰器



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

---
_Source: https://npm.io/package/note-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
