post query issue with express.js and node.js (server side) and react.js (client side)

114 Views Asked by At

I am a junior developer and I am facing an issue with a post query made in node.js, express.js while using ReactJS on the front-end.

I have two folders: client and server

In the server folder I am using node.js and express.js. Here is the code in the index.js file from the server side:

enter image description here

enter image description here

In the client folder I am using node.js and express.js. Here is the code in the App.js file from the client side:

enter image description here

My server is running on the port 3306 and the code is ok, client side as server side.

The issue I am having is with the db. I created a db in sequel pro « employeeSystem » with a table « employee ».

And I cannot insert the values in the table.

Error message 1:

Error: Connection lost: The server closed the connection. at Protocol.end (/Users/ana/Desktop/crud-tutorial/server/node_modules/mysql/lib/protocol/Protocol.js:112:13)

Error message 2: { fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } Error: Cannot enqueue Query after fatal error.

I am a bit lost on the MySQL side and with the db I created in Sequel Pro.

enter image description here

In the browser here is the localhost:3000

enter image description here

and the localhost:3306 working well

enter image description here

Thank you very much in advance if you can help!

1

There are 1 best solutions below

0
conaticus On

I know this was asked a while ago, but I'd thought I'd answer incase you're still stuck.

The issue is that you're using mysql.createConnection() which is designed to be opened and closed as soon as the query has finished, and not kept open for the lifecycle of your app.

For long lasting connections, you will want to use mysql.createPool() which is designed for connections lasting as long as the app does. Alternatively, you could open the connection when you need to interact with the database, and close the connection after the interaction has finished.

Here are some links:

createPool() - the arguments are the same as createConnection()
connection.end() - takes in a function