Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Fail to fetch POST info

I'm learning backend node/express. In this block of code I'm trying to send some data to backend but i get Uncaught (in promise) TypeError: Failed to fetch. Thank you.

Backend:

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

const publicPath = path.join(__dirname, "..","client", 'dist')
const publicPath2 = path.join(__dirname, "..","client", 'public')
const port = process.env.PORT || 3000


app.use(express.urlencoded({extended: true}))
app.use(express.json())
app.use(cors())
app.use(express.static(publicPath))
app.use(express.static(publicPath2))

app.use(express.urlencoded)


app.get("*", (req,res) => {
    res.sendFile(path.join(publicPath, 'index.html'))
})

//edit

app.post('/api', (req, res) => {
console.log('I got a req')
console.log(req.body)
const data = req.body
res.json({
    status:'success',
    cart:data.cart
})

})

app.listen(port, () => {
    console.log('server is up')
})

frontEnd

const submitAuthor = async () => {
        const result = await fetch('/api', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(cart)
        })
        const data= await result.json()
        console.log(data)
      }

Edited the response part.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your browser knows how to send only GET requests, unless you specify somehow that you need another method (like using a form). So if you just try to access that endpoint through the url, a GET request will be sent by the browser, not a POST one

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!