How to have multiple server (sapper) get, post, etc routes in a single file?

只谈情不闲聊 提交于 2019-12-13 03:53:21

问题


I am using sapper server routing and this works with individual .js files that will handle a single get, post, etc using the filename as the route and export function post(req,res,next).

I would like to use my own server routing like Express with multiple handlers in a single file like...

app.post('/api/abc', req,res,next)

app.post('/api/def', req,res,next)

Is this possible in Sapper and if so can someone please give an example?


回答1:


Add the handlers to your 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);
    });


来源:https://stackoverflow.com/questions/57751156/how-to-have-multiple-server-sapper-get-post-etc-routes-in-a-single-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!