SteveO
SteveO

Reputation: 751

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

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?

Upvotes: 3

Views: 1639

Answers (1)

Rich Harris
Rich Harris

Reputation: 29585

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);
    });

Upvotes: 7

Related Questions