├── README.md ├── node-express-server ├── .gitignore ├── README.md ├── app │ ├── config │ │ └── db.config.js │ ├── controllers │ │ └── tutorial.controller.js │ ├── models │ │ ├── index.js │ │ └── tutorial.model.js │ └── routes │ │ └── turorial.routes.js ├── package.json └── server.js └── react-client ├── .env ├── .gitignore ├── README.md ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── react-crud-example-web-api-demo.png ├── src ├── App.css ├── App.js ├── common │ └── with-router.js ├── components │ ├── add-tutorial.component.js │ ├── tutorial.component.js │ └── tutorials-list.component.js ├── http-common.js ├── index.js ├── logo.svg ├── serviceWorker.js ├── services │ └── tutorial.service.js └── setupTests.js └── yarn.lock /README.md: -------------------------------------------------------------------------------- 1 | # Integrate React with Node.js Express & MySQL (run on same Server/Port) 2 | 3 | For more detail, please visit: 4 | > [How to integrate React with Node.js Express on same Server/Port](https://bezkoder.com/integrate-react-express-same-server-port/) 5 | 6 | > [React (Components) CRUD example to consume Web API](https://bezkoder.com/react-crud-web-api/) 7 | 8 | > [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/) 9 | 10 | More Practice: 11 | > [React (Hooks) CRUD example to consume Web API](https://bezkoder.com/react-hooks-crud-axios-api/) 12 | 13 | > [React Material UI examples with a CRUD Application](https://bezkoder.com/react-material-ui-examples-crud/) 14 | 15 | > [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/) 16 | 17 | > [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/) 18 | 19 | Associations: 20 | > [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/) 21 | 22 | > [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/) 23 | 24 | Fullstack with Node.js Express: 25 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/) 26 | 27 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/) 28 | 29 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/) 30 | 31 | Security: 32 | > [React + Node.js Express + MySQL: User Authentication with JWT example](https://www.bezkoder.com/react-express-authentication-jwt/) 33 | 34 | > [React + Node.js Express + MongoDB: User Authentication with JWT example](https://www.bezkoder.com/react-node-mongodb-auth/) 35 | 36 | Serverless: 37 | > [React Firebase CRUD App with Realtime Database](https://bezkoder.com/react-firebase-crud/) 38 | 39 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://bezkoder.com/react-firestore-crud/) 40 | -------------------------------------------------------------------------------- /node-express-server/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json -------------------------------------------------------------------------------- /node-express-server/README.md: -------------------------------------------------------------------------------- 1 | # Node.js Rest APIs with Express, Sequelize & MySQL example 2 | 3 | For more detail, please visit: 4 | > [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://www.bezkoder.com/node-js-express-sequelize-mysql/) 5 | 6 | > [Build Node.js Rest APIs with Express & MySQL (without Sequelize)](https://www.bezkoder.com/node-js-rest-api-express-mysql/) 7 | 8 | > [Node.js Express File Upload Rest API example](https://www.bezkoder.com/node-js-express-file-upload/) 9 | 10 | > [Server side Pagination in Node.js with Sequelize and MySQL](https://www.bezkoder.com/node-js-sequelize-pagination-mysql/) 11 | 12 | > [Deploying/Hosting Node.js app on Heroku with MySQL database](https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/) 13 | 14 | Front-end that works well with this Back-end 15 | > [Axios Client](https://www.bezkoder.com/axios-request/) 16 | 17 | > [Angular 8](https://www.bezkoder.com/angular-crud-app/) / [Angular 10](https://www.bezkoder.com/angular-10-crud-app/) / [Angular 11](https://www.bezkoder.com/angular-11-crud-app/) / [Angular 12](https://www.bezkoder.com/angular-12-crud-app/) / [Angular 13](https://www.bezkoder.com/angular-13-crud-example/) / [Angular 14](https://www.bezkoder.com/angular-14-crud-example/) / [Angular 15](https://www.bezkoder.com/angular-15-crud-example/) / [Angular 16 Client](https://www.bezkoder.com/angular-16-crud-example/) 18 | 19 | > [Vue 2 Client](https://www.bezkoder.com/vue-js-crud-app/) / [Vue 3 Client](https://www.bezkoder.com/vue-3-crud/) / [Vuetify Client](https://www.bezkoder.com/vuetify-data-table-example/) 20 | 21 | > [React Client](https://www.bezkoder.com/react-crud-web-api/) / [React Redux Client](https://www.bezkoder.com/react-redux-crud-example/) 22 | 23 | ## More Practice 24 | 25 | Security: 26 | > [Node.js Express: JWT example | Token Based Authentication & Authorization](https://www.bezkoder.com/node-js-jwt-authentication-mysql/) 27 | 28 | Associations: 29 | > [Sequelize Associations: One-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-one-to-many/) 30 | 31 | > [Sequelize Associations: Many-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-many-to-many/) 32 | 33 | Fullstack: 34 | > [Vue.js + Node.js + Express + MySQL example](https://www.bezkoder.com/vue-js-node-js-express-mysql-crud-example/) 35 | 36 | > [Vue.js + Node.js + Express + MongoDB example](https://www.bezkoder.com/vue-node-express-mongodb-mevn-crud/) 37 | 38 | > [Angular 8 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-node-express-mysql/) 39 | 40 | > [Angular 10 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-10-node-js-express-mysql/) 41 | 42 | > [Angular 11 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-11-node-js-express-mysql/) 43 | 44 | > [Angular 12 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-12-node-js-express-mysql/) 45 | 46 | > [Angular 13 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-13-node-js-express-mysql/) 47 | 48 | > [Angular 14 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-14-node-js-express-mysql/) 49 | 50 | > [Angular 15 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-15-node-js-express-mysql/) 51 | 52 | > [Angular 16 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-16-node-js-express-mysql/) 53 | 54 | > [React + Node.js + Express + MySQL example](https://www.bezkoder.com/react-node-express-mysql/) 55 | 56 | Integration (run back-end & front-end on same server/port) 57 | > [Integrate React with Node.js Restful Services](https://www.bezkoder.com/integrate-react-express-same-server-port/) 58 | 59 | > [Integrate Angular with Node.js Restful Services](https://www.bezkoder.com/integrate-angular-10-node-js/) 60 | 61 | > [Integrate Vue with Node.js Restful Services](https://www.bezkoder.com/serve-vue-app-express/) 62 | 63 | ## Project setup 64 | ``` 65 | npm install 66 | ``` 67 | 68 | ### Run 69 | ``` 70 | node server.js 71 | ``` 72 | -------------------------------------------------------------------------------- /node-express-server/app/config/db.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | HOST: "localhost", 3 | USER: "root", 4 | PASSWORD: "123456", 5 | DB: "testdb", 6 | dialect: "mysql", 7 | pool: { 8 | max: 5, 9 | min: 0, 10 | acquire: 30000, 11 | idle: 10000 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /node-express-server/app/controllers/tutorial.controller.js: -------------------------------------------------------------------------------- 1 | const db = require("../models"); 2 | const Tutorial = db.tutorials; 3 | const Op = db.Sequelize.Op; 4 | 5 | // Create and Save a new Tutorial 6 | exports.create = (req, res) => { 7 | // Validate request 8 | if (!req.body.title) { 9 | res.status(400).send({ 10 | message: "Content can not be empty!" 11 | }); 12 | return; 13 | } 14 | 15 | // Create a Tutorial 16 | const tutorial = { 17 | title: req.body.title, 18 | description: req.body.description, 19 | published: req.body.published ? req.body.published : false 20 | }; 21 | 22 | // Save Tutorial in the database 23 | Tutorial.create(tutorial) 24 | .then(data => { 25 | res.send(data); 26 | }) 27 | .catch(err => { 28 | res.status(500).send({ 29 | message: 30 | err.message || "Some error occurred while creating the Tutorial." 31 | }); 32 | }); 33 | }; 34 | 35 | // Retrieve all Tutorials from the database. 36 | exports.findAll = (req, res) => { 37 | const title = req.query.title; 38 | var condition = title ? { title: { [Op.like]: `%${title}%` } } : null; 39 | 40 | Tutorial.findAll({ where: condition }) 41 | .then(data => { 42 | res.send(data); 43 | }) 44 | .catch(err => { 45 | res.status(500).send({ 46 | message: 47 | err.message || "Some error occurred while retrieving tutorials." 48 | }); 49 | }); 50 | }; 51 | 52 | // Find a single Tutorial with an id 53 | exports.findOne = (req, res) => { 54 | const id = req.params.id; 55 | 56 | Tutorial.findByPk(id) 57 | .then(data => { 58 | if (data) { 59 | res.send(data); 60 | } else { 61 | res.status(404).send({ 62 | message: `Cannot find Tutorial with id=${id}.` 63 | }); 64 | } 65 | }) 66 | .catch(err => { 67 | res.status(500).send({ 68 | message: "Error retrieving Tutorial with id=" + id 69 | }); 70 | }); 71 | }; 72 | 73 | // Update a Tutorial by the id in the request 74 | exports.update = (req, res) => { 75 | const id = req.params.id; 76 | 77 | Tutorial.update(req.body, { 78 | where: { id: id } 79 | }) 80 | .then(num => { 81 | if (num == 1) { 82 | res.send({ 83 | message: "Tutorial was updated successfully." 84 | }); 85 | } else { 86 | res.send({ 87 | message: `Cannot update Tutorial with id=${id}. Maybe Tutorial was not found or req.body is empty!` 88 | }); 89 | } 90 | }) 91 | .catch(err => { 92 | res.status(500).send({ 93 | message: "Error updating Tutorial with id=" + id 94 | }); 95 | }); 96 | }; 97 | 98 | // Delete a Tutorial with the specified id in the request 99 | exports.delete = (req, res) => { 100 | const id = req.params.id; 101 | 102 | Tutorial.destroy({ 103 | where: { id: id } 104 | }) 105 | .then(num => { 106 | if (num == 1) { 107 | res.send({ 108 | message: "Tutorial was deleted successfully!" 109 | }); 110 | } else { 111 | res.send({ 112 | message: `Cannot delete Tutorial with id=${id}. Maybe Tutorial was not found!` 113 | }); 114 | } 115 | }) 116 | .catch(err => { 117 | res.status(500).send({ 118 | message: "Could not delete Tutorial with id=" + id 119 | }); 120 | }); 121 | }; 122 | 123 | // Delete all Tutorials from the database. 124 | exports.deleteAll = (req, res) => { 125 | Tutorial.destroy({ 126 | where: {}, 127 | truncate: false 128 | }) 129 | .then(nums => { 130 | res.send({ message: `${nums} Tutorials were deleted successfully!` }); 131 | }) 132 | .catch(err => { 133 | res.status(500).send({ 134 | message: 135 | err.message || "Some error occurred while removing all tutorials." 136 | }); 137 | }); 138 | }; 139 | 140 | // find all published Tutorial 141 | exports.findAllPublished = (req, res) => { 142 | Tutorial.findAll({ where: { published: true } }) 143 | .then(data => { 144 | res.send(data); 145 | }) 146 | .catch(err => { 147 | res.status(500).send({ 148 | message: 149 | err.message || "Some error occurred while retrieving tutorials." 150 | }); 151 | }); 152 | }; 153 | -------------------------------------------------------------------------------- /node-express-server/app/models/index.js: -------------------------------------------------------------------------------- 1 | const dbConfig = require("../config/db.config.js"); 2 | 3 | const Sequelize = require("sequelize"); 4 | const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, { 5 | host: dbConfig.HOST, 6 | dialect: dbConfig.dialect, 7 | operatorsAliases: false, 8 | 9 | pool: { 10 | max: dbConfig.pool.max, 11 | min: dbConfig.pool.min, 12 | acquire: dbConfig.pool.acquire, 13 | idle: dbConfig.pool.idle 14 | } 15 | }); 16 | 17 | const db = {}; 18 | 19 | db.Sequelize = Sequelize; 20 | db.sequelize = sequelize; 21 | 22 | db.tutorials = require("./tutorial.model.js")(sequelize, Sequelize); 23 | 24 | module.exports = db; 25 | -------------------------------------------------------------------------------- /node-express-server/app/models/tutorial.model.js: -------------------------------------------------------------------------------- 1 | module.exports = (sequelize, Sequelize) => { 2 | const Tutorial = sequelize.define("tutorial", { 3 | title: { 4 | type: Sequelize.STRING 5 | }, 6 | description: { 7 | type: Sequelize.STRING 8 | }, 9 | published: { 10 | type: Sequelize.BOOLEAN 11 | } 12 | }); 13 | 14 | return Tutorial; 15 | }; 16 | -------------------------------------------------------------------------------- /node-express-server/app/routes/turorial.routes.js: -------------------------------------------------------------------------------- 1 | module.exports = app => { 2 | const tutorials = require("../controllers/tutorial.controller.js"); 3 | 4 | var router = require("express").Router(); 5 | 6 | // Create a new Tutorial 7 | router.post("/", tutorials.create); 8 | 9 | // Retrieve all Tutorials 10 | router.get("/", tutorials.findAll); 11 | 12 | // Retrieve all published Tutorials 13 | router.get("/published", tutorials.findAllPublished); 14 | 15 | // Retrieve a single Tutorial with id 16 | router.get("/:id", tutorials.findOne); 17 | 18 | // Update a Tutorial with id 19 | router.put("/:id", tutorials.update); 20 | 21 | // Delete a Tutorial with id 22 | router.delete("/:id", tutorials.delete); 23 | 24 | // Delete all Tutorials 25 | router.delete("/", tutorials.deleteAll); 26 | 27 | app.use('/api/tutorials', router); 28 | }; 29 | -------------------------------------------------------------------------------- /node-express-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nodejs-express-sequelize-mysql", 3 | "version": "1.0.0", 4 | "description": "Node.js Rest Apis with Express, Sequelize & MySQL", 5 | "main": "server.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "nodejs", 11 | "express", 12 | "rest", 13 | "api", 14 | "sequelize", 15 | "mysql" 16 | ], 17 | "author": "bezkoder", 18 | "license": "ISC", 19 | "dependencies": { 20 | "cors": "^2.8.5", 21 | "express": "^4.18.2", 22 | "mysql2": "^2.3.3", 23 | "sequelize": "^6.32.0" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /node-express-server/server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const cors = require("cors"); 3 | 4 | const app = express(); 5 | 6 | var corsOptions = { 7 | origin: "http://localhost:8081" 8 | }; 9 | 10 | app.use(cors(corsOptions)); 11 | 12 | // parse requests of content-type - application/json 13 | app.use(express.json()); 14 | 15 | // parse requests of content-type - application/x-www-form-urlencoded 16 | app.use(express.urlencoded({ extended: true })); 17 | 18 | const db = require("./app/models"); 19 | 20 | db.sequelize.sync() 21 | .then(() => { 22 | console.log("Synced db."); 23 | }) 24 | .catch((err) => { 25 | console.log("Failed to sync db: " + err.message); 26 | }); 27 | 28 | // // drop the table if it already exists 29 | // db.sequelize.sync({ force: true }).then(() => { 30 | // console.log("Drop and re-sync db."); 31 | // }); 32 | 33 | // simple route 34 | app.get("/", (req, res) => { 35 | res.json({ message: "Welcome to bezkoder application." }); 36 | }); 37 | 38 | require("./app/routes/turorial.routes")(app); 39 | 40 | // set port, listen for requests 41 | const PORT = process.env.PORT || 8080; 42 | app.listen(PORT, () => { 43 | console.log(`Server is running on port ${PORT}.`); 44 | }); 45 | -------------------------------------------------------------------------------- /react-client/.env: -------------------------------------------------------------------------------- 1 | PORT=8081 -------------------------------------------------------------------------------- /react-client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /react-client/README.md: -------------------------------------------------------------------------------- 1 | # React.js CRUD App with React Router & Axios 2 | 3 | Build a React.js CRUD Application to consume Web API, display and modify data with Router, Axios & Bootstrap. 4 | 5 | React Tutorial Application in that: 6 | - Each Tutorial has id, title, description, published status. 7 | - We can create, retrieve, update, delete Tutorials. 8 | - There is a Search bar for finding Tutorials by title. 9 | 10 |  11 | 12 | For instruction, please visit: 13 | > [React CRUD example to consume Web API](https://bezkoder.com/react-crud-web-api/) 14 | 15 | Related Posts: 16 | > [React Typescript CRUD example to consume Web API](https://bezkoder.com/react-typescript-axios/) 17 | 18 | > [React Redux CRUD App example with Rest API](https://bezkoder.com/react-redux-crud-example/) 19 | 20 | > [React (Hooks) CRUD example to consume Web API](https://bezkoder.com/react-hooks-crud-axios-api/) 21 | 22 | > [React Table example: CRUD App with react-table v7](https://bezkoder.com/react-table-example-hooks-crud/) 23 | 24 | Using Material UI instead of Bootstrap: 25 | > [React Material UI examples with a CRUD Application](https://bezkoder.com/react-material-ui-examples-crud/) 26 | 27 | More Practice: 28 | > [React Pagination example](https://bezkoder.com/react-pagination-material-ui/) 29 | 30 | > [React File Upload example](https://bezkoder.com/react-file-upload-axios/) 31 | 32 | > [React JWT Authentication & Authorization example](https://bezkoder.com/react-jwt-auth/) 33 | 34 | > [React + Redux: JWT Authentication & Authorization example](https://bezkoder.com/react-redux-jwt-auth/) 35 | 36 | Fullstack with Node.js Express: 37 | > [React.js + Node.js Express + MySQL](https://bezkoder.com/react-node-express-mysql/) 38 | 39 | > [React.js + Node.js Express + PostgreSQL](https://bezkoder.com/react-node-express-postgresql/) 40 | 41 | > [React.js + Node.js Express + MongoDB](https://bezkoder.com/react-node-express-mongodb-mern-stack/) 42 | 43 | Fullstack with Spring Boot: 44 | > [React.js + Spring Boot + MySQL](https://bezkoder.com/react-spring-boot-crud/) 45 | 46 | > [React.js + Spring Boot + PostgreSQL](https://bezkoder.com/spring-boot-react-postgresql/) 47 | 48 | > [React.js + Spring Boot + MongoDB](https://bezkoder.com/react-spring-boot-mongodb/) 49 | 50 | Fullstack with Django: 51 | 52 | > [React.js + Django Rest Framework](https://bezkoder.com/django-react-axios-rest-framework/) 53 | 54 | Serverless: 55 | > [React Firebase CRUD App with Realtime Database](https://bezkoder.com/react-firebase-crud/) 56 | 57 | > [React Firestore CRUD App example | Firebase Cloud Firestore](https://bezkoder.com/react-firestore-crud/) 58 | 59 | Integration (run back-end & front-end on same server/port) 60 | > [How to integrate React.js with Spring Boot](https://bezkoder.com/integrate-reactjs-spring-boot/) 61 | 62 | > [Integrate React with Node.js Express on same Server/Port](https://bezkoder.com/integrate-react-express-same-server-port/) 63 | 64 | 65 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 66 | 67 | ### Set port 68 | .env 69 | ``` 70 | PORT=8081 71 | ``` 72 | 73 | ## Project setup 74 | 75 | In the project directory, you can run: 76 | 77 | ``` 78 | npm install 79 | # or 80 | yarn install 81 | ``` 82 | 83 | or 84 | 85 | ### Compiles and hot-reloads for development 86 | 87 | ``` 88 | npm start 89 | # or 90 | yarn start 91 | ``` 92 | 93 | Open [http://localhost:8081](http://localhost:8081) to view it in the browser. 94 | 95 | The page will reload if you make edits. 96 | -------------------------------------------------------------------------------- /react-client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-crud", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.16.4", 7 | "@testing-library/react": "^13.0.1", 8 | "@testing-library/user-event": "^14.1.1", 9 | "axios": "^0.27.2", 10 | "bootstrap": "^4.6.2", 11 | "react": "^18.2.0", 12 | "react-dom": "^18.2.0", 13 | "react-router-dom": "^6.4.0", 14 | "react-scripts": "5.0.1" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": "react-app" 24 | }, 25 | "browserslist": { 26 | "production": [ 27 | ">0.2%", 28 | "not dead", 29 | "not op_mini all" 30 | ], 31 | "development": [ 32 | "last 1 chrome version", 33 | "last 1 firefox version", 34 | "last 1 safari version" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /react-client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mierudayo/React-sample-guide/ccc203a004c310bc5d887e1b71f0279a812fd30b/react-client/public/favicon.ico -------------------------------------------------------------------------------- /react-client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 |{this.state.message}
186 |Please click on a Tutorial...
191 |Please click on a Tutorial...
177 |