fetch post is giving me undefined for the posted data?

后端 未结 1 2021
小鲜肉
小鲜肉 2021-01-25 14:01

Learning how to use Sapper. I have a component with form (the form has one field to enter an email address) and use fetch to post the data to the serverhandle. When I post the d

相关标签:
1条回答
  • 2021-01-25 14:02

    I had to npm install body-parser --save and add it to the server.js. I had to add bodyParser.urlencoded and bodyParser.json to get it to work.

    import sirv from 'sirv';
    import polka from 'polka';
    import compression from 'compression';
    import * as sapper from '@sapper/server';
    
    // const express = require('express')
    const app = polka()
    const bodyParser = require('body-parser')
    
    const { PORT, NODE_ENV } = process.env;
    const dev = NODE_ENV === 'development';
    
    app.use(bodyParser.urlencoded({ extended: false }))
    app.use(bodyParser.json())
    
    app
      .use(
        compression({ threshold: 0 }),
        sirv('static', { dev }),
        sapper.middleware()
      )
      .listen(PORT, err => {
        if (err) console.log('error', err);
      });
    
    0 讨论(0)
提交回复
热议问题