svelte - 如何在一个文件中拥有多个服务器(工兵)获取、发布等路由?
问题描述
我正在使用 sapper 服务器路由,这适用于单个 .js 文件,这些文件将使用文件名作为路由和导出函数 post(req,res,next) 处理单个 get、post 等。
我想在一个文件中使用我自己的服务器路由,比如 Express 和多个处理程序,比如......
app.post('/api/abc', req,res,next)
app.post('/api/def', req,res,next)
这在 Sapper 中是否可行,如果可以,有人可以举个例子吗?
解决方案
将处理程序添加到您的server.js:
polka() // Or `express()`, if you're using that
/* add your handlers here */
.post('/api/abc', (req, res, next) => {...})
.post('/api/def', (req, res, next) => {...})
/* normal stuff */
.use(
compression({ threshold: 0 }),
sirv('static', { dev }),
sapper.middleware()
)
.listen(PORT, err => {
if (err) console.log('error', err);
});