Answer the question
In order to leave comments, you need to log in
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)
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)
})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question