Remix 是一个全栈框架,能写 api 接口的能必定是具备的,而且 Remix 尽管定义为全栈框架,此文章次要摸索如何接口。

本文内容

  • Remix 中 api 的书写形式
  • Remix 中 RESTful api 的属性形式
  • 类 MVC 分层接口如何写 Remix
  • 解决上传示例

接口品种

  • 一般 get/post api:即可满足根本
  • RESTful API:有标准, 须要协同的
  • graphql:可管制须要的字段的,良好的跨端

其中如果是一些小我的项目,没有必要规则化的我的项目,应用 get/post 解决就曾经足够了,如果我的项目有了很多的人要保护,并且有了肯定的规模,为了方便管理,能够应用 RESTful API 的形式解决。graphql API 手动能力最强。

RESTful API 特点

  • 创立:POST /api/resources
  • 读取:GET /api/resources/:id
  • 更新:PUT /api/resources/:id
  • 删除:DELETE /api/resources/:id

其中,:id 示意资源的惟一标识符。

Remix 中如何解决 api 特点

  • loader 解决 get 申请
  • action 解决非 get 申请

Loader 函数解决 Get 申请

export const loader = () => {  return json({ get: 'loader get' })}

action 解决非 GET 办法

import { json } from "@remix-run/node";const handleNotGetRequest = function ({ request }) {  const method = request.method;  switch (method) {    case "POST":      return json({ code: 0, method: "POST", message: "增加胜利" });    case "PUT":      return json({ ok: true, code: 1, method: "PUT", message: "批改胜利" });    case "DELETE":      return json({ ok: true, code: 1, method: "PUT", message: "删除胜利" });    default:      break;  }};// 非 getexport const action = ({ request }) => {  return handleNotGetRequest({ request });};//  getexport const loader = ({ request }) => {  return json({    a: 1,  });};

增加管制层和服务层

为了代码更加好保护,有构造的代码时必要的,代码分层占据重要地位。

如果应用 mongoose 等会定义模型层,定义操作数据的模型,而后应用管制层来操作模型层。形成一个简略类 MVC 分层构造。当然 Remix 是一个基于 React + Node.js 全栈框架,应用模型层+服务层:

应用 mongoose 定义模型层, category.model.ts

import mongoose from 'mongoose'const CategorySchema = new mongoose.Schema({  name: String,  description: String,  createdAt: Date,  articleIds: [String]})export default mongoose.models.Category ||  mongoose.model('Category', CategorySchema)

应用 category.service.ts 定义服务层,提供给 Remix loader 和 action 操作数据应用

// modelimport Category from '~/models/category.model'export const delCategoryById = async (_id: string) => {  return await Category.remove({ _id })}export const findCategoryByName = async (name: string) => {  return await Category.findOne({ name })}export const updateCategoryById = async (_id: string, update: any) => {  return await Category.findByIdAndUpdate({ _id }, update)}export const findCategoryById = async (_id: string) => {  return await Category.findOne({ _id })}export const addCategoryByName = async (name: string) => {  const CategoryEntify = new Category({ name, createdAt: new Date() })  return await CategoryEntify.save()}

裸露 loader 接口

// coreimport { json } from '@remix-run/node'// serviceimport * as categoryService from '~/services/category.service'// remix loaderexport async function loader() {  const list = await categoryService    .getCategoryList({}, '_id createdAt name articleIds', 0, 100000)    .then((list) => list)  return json({ code: 0, message: 'success', data: { list } }, 200)}

在 loader 函数中通过 services 层来获取数据,而后应用 json 函数返回数据。

应用 action 办法解决文件上传接口

  • api.upload.files.tsx 上传文件到本地
import type { ActionArgs } from '@remix-run/node'import {  json,  unstable_composeUploadHandlers as composeUploadHandlers,  unstable_createFileUploadHandler as createFileUploadHandler,  unstable_createMemoryUploadHandler as createMemoryUploadHandler,  unstable_parseMultipartFormData as parseMultipartFormData} from '@remix-run/node'// single file uploadexport const action = async ({ request }: ActionArgs) => {  const uploadHandler = composeUploadHandlers(    createFileUploadHandler({      directory: 'public/uploads', // 指定上传目录      maxPartSize: 30000000, //  指定大小      file: (file) => {        return file.filename      }    }),    createMemoryUploadHandler()  )  const formData = await parseMultipartFormData(request, uploadHandler)  return json({ imgSrc: formData.get('file') }) // 返回文件名}

上传应用 post 办法,在 action 函数应用表单形式进行解决。

小结

本文次要关注点是与 Node.js 其余的框架有什么不同。loader 解决 get 办法,action 解决非 get 申请。从一般的申请解决到解决分层的过程,写好一个 api 的学习变动过程。