W
W
wakenbyWork2022-02-17 19:00:07
JavaScript
wakenbyWork, 2022-02-17 19:00:07

How to get data in express.js from post formData?

I wrote a simple code in express:

const express = require('express')
const cors = require('cors')
const app = express()

const corsOptions = {
  origin: 'http://localhost:3000'
}

app.post('/subscribe', cors(corsOptions), function (req, res) {
  console.log(req.body)
})

app.listen(1000)


And this is how I send a request from the front:

onDelegateSubmit('[data-form="chimp"]', async function (event) {
  event.preventDefault()

  const formData = new FormData(this)
  const email = formData.get('email')
  const pattern = /^([a-z0-9_\.-])[email protected][a-z0-9-]+\.([a-z]{2,4}\.)?[a-z]{2,4}$/i

  if (email.trim() === '') return alert('Пустое поле((')
  if (!pattern.test(email)) return alert('Не валидная почта((')

  const response = await fetch('http://localhost:1000/subscribe', { method: 'post', body: formData })
  const data = await response.json()

  console.log(data)
})


And I can't figure out how to get data in express.js from formData. Don't tell me how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2022-02-17
@Lynn

Read documentation on express.urlencoded

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question