L
L
lucky42020-07-26 11:09:18
Node.js
lucky4, 2020-07-26 11:09:18

How to connect React with NodeJS?

Trying to make a simple React connection with NodeJS.

React backend connection:

axios({
      method: 'GET',
      url: 'http://localhost:8000',
      headers: {
        "Content-Type": "application/json"
      }
    }).then(response => {
      console.log(response.data.message)
    })


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

app.use(cors)

app.get("/", (request, response) => {
    response.send({ message: "We did it!" })
})

app.set('port', process.env.PORT || 8000)
app.listen(app.get('port'), () => {
    console.log(`Server is running on PORT: ${app.get('port')}`)
})


No mistakes, no. But, I noticed that in devtools the server node is pending. Or is it due to cors?
5f1d39e4d85a4817286348.png

What's the problem, how can I display a simple background text?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-07-26
@lucky4

app.use(cors())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question