跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Node.js] 2 分钟完成文件上传接口, 3 分钟完成一个 CURD 接口,轻量框架 fast-qjs(开源)

发表于

什么是 fast-qjs ?它能做什么? fast-qjs 是一个基于 fastify 框架使用约定式进行路由约束的轻量框架,完全继承 fastfiy 的优点,当你想要快速实现一个功能模块的 crud ,那么 fast-qjs 就能让你在 10 分钟以内完成

fast-qjs 优点:

  1. 内置数据库:无需繁琐的数据库配置,即可直接使用。
  2. 集成 JWT:方便且安全的用户身份验证功能。
  3. 便捷的文件上传接口:轻松处理文件,自动化存储管理。
  4. 公共目录支持:提供静态资源的管理和访问。
  5. 约定式路由管理:简洁的路由设置,高效的资源调度。

项目地址fast-qjs

下面以 fast-qjs 提供的files接口进行一个案例

第一步,确保已经安装了nodejs,终端运行:

npx create-qjs qjs-app

执行成功你将看到如下项目结构

qjs-app/
├── node_modules/       # 依赖包目录
│
├── package.json        # 项目配置文件
│
├── package-lock.json   # 依赖版本信息       

紧接着执行下面的命令

cd qjs-app && npm run start

执行完你将看到如下结构

qjs-app/
│
├── db/                 
│
├── functions/         
│
├── public/             
│
├── node_modules/      
│
├── package.json       
│
├── package-lock.json      

现在进入functions目录,你将看到一个默认的qjs.mjs文件,根据终端提示你的项目运行在http://localhost:5173。紧接着你的 api 已经创建,浏览器访问 http://localhost:5173/api/qjs 你将看 qjs.mjs 默认插入数据库的信息

我们打开qjs.mjs文件你将看到如下信息:

// functions/qjs.mjs

// @see https://github.com/allmors/qjs/
// import qjs from '@allmors/qjs/core';

export default async function (params, ctx) {
    // const User = await qjs.db.collection('user');
    // const user = await User.insertOne({ name: "Sam", email: 'sam@codingsamrat.com' })

    // or 

    const User = await this.db.collection('user');
    const user = await User.insertOne({ name: "Sam", email: 'sam@codingsamrat.com' })
    // ......

    return ctx.reply.send({
        message: 'Hello from qjs API',
        method: context.method,
        params: {
            ...user
        }
    });
}

默认代码不用管,直接粘贴下面代码全部替换

export default async function (params, ctx) {
    try {
        const { file } = params
        let result = {}
        if (ctx.method === 'POST') {
            const res = await this.files.upload(file)
            result = res
        }
        return ctx.reply.send({
            message: 'Hello from qjs API',
            method: ctx.method,
            result: result
        });
    } catch (error) {
        throw new Error(error)
    }
}

现在打开 apifox 或者其它工具,我以 apifox 为例 image.png

现在你的文件上传接口已经开发完成,回到你的项目目录你将会看到uploads,里面就是你上传的文件,你可用通过 nginx 获取 caddy 反代该目录即可访问你的文件,当然了,如果你不想这么麻烦,那么,修改 package.json 中如下配置:

找到QJS_STATIC,把QJS_STATIC修改为uploads即可,然后访问:http:0.0.0.0:5173/public/你的 uploads 目录里面的文件名

比如:http://0.0.0.0:5173/public/61f1adee-08ea-45-1727658693615/deKGMl9.jpg


更多信息请查阅文档fast-qjs

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.