npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

note-server

v1.0.0

Published

node server

Downloads

2

Readme

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 修饰器

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