问题
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