Tell us what’s happening:
error message in console as follows:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3003/posts. (Reason: CORS request did not succeed).
Your code so far
**reactjs code so far**
//Contact.js
import React, { Component } from 'react';
// import ReactDOM from 'react-dom';
class Contact extends Component {
constructor(props) {
super(props);
this.state = { posts: [] };
fetch('http://localhost:3003/posts')
.then(response => response.json())
.then(posts => (this.setState({posts}))
)}
render() {
return (<div>
Hello World
<ul>
{this.state.posts.map(post => <li>
<h2>{post.id}</h2>
<p>{post.name}</p>
</li>)}
</ul>
</div>);
}
}
// ReactDOM.render(
// <Contact />,
// mountNode
// );
export default Contact
//App.js
import React, { Component } from 'react';
import Contact from './Component/Contact';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<Contact></Contact>
</div>
);
}
}
export default App;
//index2.js
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
// https://github.com/mysqljs/mysql
const connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'vanitha'
});
// Initialize the app
const app = express();
// https://expressjs.com/en/guide/routing.html
app.get('/posts', function (req, res) {
connection.connect();
connection.query('SELECT * FROM posts', function (error, results, fields) {
if (error) throw error;
res.send(results)
});
connection.end();
});
// Start the server
app.listen(3003, () => {
console.log('Go to http://localhost:3003/posts to see posts');
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
.