lwgo
V2EX  ›  Next.js

关于 Nextjs app router 做全栈写后端的 API 不友好的问题

  •  
  •   lwgo · Apr 24, 2024 · 3125 views
    This topic created in 781 days ago, the information mentioned may be changed or developed.

    最近在看 nextJs, 假如用 NextJS 去做全栈的话,感觉对于在后端端 API 上不太友好呢.比如最新默认支持的 App Router 的话.基于文件路径路由,route.js 里面只能写 get post 等方法.

    假如我要写一个用户的操作,首先可以有基础用户的增删改查/api/user/route.js 写 get post put 这种, 如果想要在用户里面增加一些统计,或者 getById listByType disableUser 这种关于用户的其他操作以行文改如何定义呢,难道每一个方法都要写一个路径吗?有没有什么方式可以实现呢?

    3 replies    2024-11-28 14:41:11 +08:00
    C64NRD
        2
    C64NRD  
       Apr 24, 2024
    IXYYI
        3
    IXYYI  
       Nov 28, 2024
    API 没有外部需求,也不用 trp 的话,可以用 server action 来进行表单提交
    https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3567 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 10:56 · PVG 18:56 · LAX 03:56 · JFK 06:56
    ♥ Do have faith in what you're doing.