├── 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 | ![react-crud-example-web-api-demo](react-crud-example-web-api-demo.png) 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 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /react-client/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mierudayo/React-sample-guide/ccc203a004c310bc5d887e1b71f0279a812fd30b/react-client/public/logo192.png -------------------------------------------------------------------------------- /react-client/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mierudayo/React-sample-guide/ccc203a004c310bc5d887e1b71f0279a812fd30b/react-client/public/logo512.png -------------------------------------------------------------------------------- /react-client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /react-client/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /react-client/react-crud-example-web-api-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mierudayo/React-sample-guide/ccc203a004c310bc5d887e1b71f0279a812fd30b/react-client/react-crud-example-web-api-demo.png -------------------------------------------------------------------------------- /react-client/src/App.css: -------------------------------------------------------------------------------- 1 | .list { 2 | text-align: left; 3 | max-width: 750px; 4 | margin: auto; 5 | } 6 | 7 | .submit-form { 8 | max-width: 300px; 9 | margin: auto; 10 | } 11 | 12 | .edit-form { 13 | max-width: 300px; 14 | margin: auto; 15 | } 16 | -------------------------------------------------------------------------------- /react-client/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { Routes, Route, Link } from "react-router-dom"; 3 | import "bootstrap/dist/css/bootstrap.min.css"; 4 | import "./App.css"; 5 | 6 | import AddTutorial from "./components/add-tutorial.component"; 7 | import Tutorial from "./components/tutorial.component"; 8 | import TutorialsList from "./components/tutorials-list.component"; 9 | 10 | class App extends Component { 11 | render() { 12 | return ( 13 |
14 | 31 | 32 |
33 | 34 | } /> 35 | } /> 36 | } /> 37 | } /> 38 | 39 |
40 |
41 | ); 42 | } 43 | } 44 | 45 | export default App; 46 | -------------------------------------------------------------------------------- /react-client/src/common/with-router.js: -------------------------------------------------------------------------------- 1 | import { useLocation, useNavigate, useParams } from "react-router-dom"; 2 | 3 | export const withRouter = (Component) => { 4 | function ComponentWithRouterProp(props) { 5 | let location = useLocation(); 6 | let navigate = useNavigate(); 7 | let params = useParams(); 8 | return ; 9 | } 10 | return ComponentWithRouterProp; 11 | }; 12 | -------------------------------------------------------------------------------- /react-client/src/components/add-tutorial.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | 4 | export default class AddTutorial extends Component { 5 | constructor(props) { 6 | super(props); 7 | this.onChangeTitle = this.onChangeTitle.bind(this); 8 | this.onChangeDescription = this.onChangeDescription.bind(this); 9 | this.saveTutorial = this.saveTutorial.bind(this); 10 | this.newTutorial = this.newTutorial.bind(this); 11 | 12 | this.state = { 13 | id: null, 14 | title: "", 15 | description: "", 16 | published: false, 17 | 18 | submitted: false 19 | }; 20 | } 21 | 22 | onChangeTitle(e) { 23 | this.setState({ 24 | title: e.target.value 25 | }); 26 | } 27 | 28 | onChangeDescription(e) { 29 | this.setState({ 30 | description: e.target.value 31 | }); 32 | } 33 | 34 | saveTutorial() { 35 | var data = { 36 | title: this.state.title, 37 | description: this.state.description 38 | }; 39 | 40 | TutorialDataService.create(data) 41 | .then(response => { 42 | this.setState({ 43 | id: response.data.id, 44 | title: response.data.title, 45 | description: response.data.description, 46 | published: response.data.published, 47 | 48 | submitted: true 49 | }); 50 | console.log(response.data); 51 | }) 52 | .catch(e => { 53 | console.log(e); 54 | }); 55 | } 56 | 57 | newTutorial() { 58 | this.setState({ 59 | id: null, 60 | title: "", 61 | description: "", 62 | published: false, 63 | 64 | submitted: false 65 | }); 66 | } 67 | 68 | render() { 69 | return ( 70 |
71 | {this.state.submitted ? ( 72 |
73 |

You submitted successfully!

74 | 77 |
78 | ) : ( 79 |
80 |
81 | 82 | 91 |
92 | 93 |
94 | 95 | 104 |
105 | 106 | 109 |
110 | )} 111 |
112 | ); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /react-client/src/components/tutorial.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | import { withRouter } from '../common/with-router'; 4 | 5 | class Tutorial extends Component { 6 | constructor(props) { 7 | super(props); 8 | this.onChangeTitle = this.onChangeTitle.bind(this); 9 | this.onChangeDescription = this.onChangeDescription.bind(this); 10 | this.getTutorial = this.getTutorial.bind(this); 11 | this.updatePublished = this.updatePublished.bind(this); 12 | this.updateTutorial = this.updateTutorial.bind(this); 13 | this.deleteTutorial = this.deleteTutorial.bind(this); 14 | 15 | this.state = { 16 | currentTutorial: { 17 | id: null, 18 | title: "", 19 | description: "", 20 | published: false 21 | }, 22 | message: "" 23 | }; 24 | } 25 | 26 | componentDidMount() { 27 | this.getTutorial(this.props.router.params.id); 28 | } 29 | 30 | onChangeTitle(e) { 31 | const title = e.target.value; 32 | 33 | this.setState(function(prevState) { 34 | return { 35 | currentTutorial: { 36 | ...prevState.currentTutorial, 37 | title: title 38 | } 39 | }; 40 | }); 41 | } 42 | 43 | onChangeDescription(e) { 44 | const description = e.target.value; 45 | 46 | this.setState(prevState => ({ 47 | currentTutorial: { 48 | ...prevState.currentTutorial, 49 | description: description 50 | } 51 | })); 52 | } 53 | 54 | getTutorial(id) { 55 | TutorialDataService.get(id) 56 | .then(response => { 57 | this.setState({ 58 | currentTutorial: response.data 59 | }); 60 | console.log(response.data); 61 | }) 62 | .catch(e => { 63 | console.log(e); 64 | }); 65 | } 66 | 67 | updatePublished(status) { 68 | var data = { 69 | id: this.state.currentTutorial.id, 70 | title: this.state.currentTutorial.title, 71 | description: this.state.currentTutorial.description, 72 | published: status 73 | }; 74 | 75 | TutorialDataService.update(this.state.currentTutorial.id, data) 76 | .then(response => { 77 | this.setState(prevState => ({ 78 | currentTutorial: { 79 | ...prevState.currentTutorial, 80 | published: status 81 | } 82 | })); 83 | console.log(response.data); 84 | }) 85 | .catch(e => { 86 | console.log(e); 87 | }); 88 | } 89 | 90 | updateTutorial() { 91 | TutorialDataService.update( 92 | this.state.currentTutorial.id, 93 | this.state.currentTutorial 94 | ) 95 | .then(response => { 96 | console.log(response.data); 97 | this.setState({ 98 | message: "The tutorial was updated successfully!" 99 | }); 100 | }) 101 | .catch(e => { 102 | console.log(e); 103 | }); 104 | } 105 | 106 | deleteTutorial() { 107 | TutorialDataService.delete(this.state.currentTutorial.id) 108 | .then(response => { 109 | console.log(response.data); 110 | this.props.router.navigate('/tutorials'); 111 | }) 112 | .catch(e => { 113 | console.log(e); 114 | }); 115 | } 116 | 117 | render() { 118 | const { currentTutorial } = this.state; 119 | 120 | return ( 121 |
122 | {currentTutorial ? ( 123 |
124 |

Tutorial

125 |
126 |
127 | 128 | 135 |
136 |
137 | 138 | 145 |
146 | 147 |
148 | 151 | {currentTutorial.published ? "Published" : "Pending"} 152 |
153 |
154 | 155 | {currentTutorial.published ? ( 156 | 162 | ) : ( 163 | 169 | )} 170 | 171 | 177 | 178 | 185 |

{this.state.message}

186 |
187 | ) : ( 188 |
189 |
190 |

Please click on a Tutorial...

191 |
192 | )} 193 |
194 | ); 195 | } 196 | } 197 | 198 | export default withRouter(Tutorial); -------------------------------------------------------------------------------- /react-client/src/components/tutorials-list.component.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import TutorialDataService from "../services/tutorial.service"; 3 | import { Link } from "react-router-dom"; 4 | 5 | export default class TutorialsList extends Component { 6 | constructor(props) { 7 | super(props); 8 | this.onChangeSearchTitle = this.onChangeSearchTitle.bind(this); 9 | this.retrieveTutorials = this.retrieveTutorials.bind(this); 10 | this.refreshList = this.refreshList.bind(this); 11 | this.setActiveTutorial = this.setActiveTutorial.bind(this); 12 | this.removeAllTutorials = this.removeAllTutorials.bind(this); 13 | this.searchTitle = this.searchTitle.bind(this); 14 | 15 | this.state = { 16 | tutorials: [], 17 | currentTutorial: null, 18 | currentIndex: -1, 19 | searchTitle: "" 20 | }; 21 | } 22 | 23 | componentDidMount() { 24 | this.retrieveTutorials(); 25 | } 26 | 27 | onChangeSearchTitle(e) { 28 | const searchTitle = e.target.value; 29 | 30 | this.setState({ 31 | searchTitle: searchTitle 32 | }); 33 | } 34 | 35 | retrieveTutorials() { 36 | TutorialDataService.getAll() 37 | .then(response => { 38 | this.setState({ 39 | tutorials: response.data 40 | }); 41 | console.log(response.data); 42 | }) 43 | .catch(e => { 44 | console.log(e); 45 | }); 46 | } 47 | 48 | refreshList() { 49 | this.retrieveTutorials(); 50 | this.setState({ 51 | currentTutorial: null, 52 | currentIndex: -1 53 | }); 54 | } 55 | 56 | setActiveTutorial(tutorial, index) { 57 | this.setState({ 58 | currentTutorial: tutorial, 59 | currentIndex: index 60 | }); 61 | } 62 | 63 | removeAllTutorials() { 64 | TutorialDataService.deleteAll() 65 | .then(response => { 66 | console.log(response.data); 67 | this.refreshList(); 68 | }) 69 | .catch(e => { 70 | console.log(e); 71 | }); 72 | } 73 | 74 | searchTitle() { 75 | this.setState({ 76 | currentTutorial: null, 77 | currentIndex: -1 78 | }); 79 | 80 | TutorialDataService.findByTitle(this.state.searchTitle) 81 | .then(response => { 82 | this.setState({ 83 | tutorials: response.data 84 | }); 85 | console.log(response.data); 86 | }) 87 | .catch(e => { 88 | console.log(e); 89 | }); 90 | } 91 | 92 | render() { 93 | const { searchTitle, tutorials, currentTutorial, currentIndex } = this.state; 94 | 95 | return ( 96 |
97 |
98 |
99 | 106 |
107 | 114 |
115 |
116 |
117 |
118 |

Tutorials List

119 | 120 |
    121 | {tutorials && 122 | tutorials.map((tutorial, index) => ( 123 |
  • this.setActiveTutorial(tutorial, index)} 129 | key={index} 130 | > 131 | {tutorial.title} 132 |
  • 133 | ))} 134 |
135 | 136 | 142 |
143 |
144 | {currentTutorial ? ( 145 |
146 |

Tutorial

147 |
148 | {" "} 151 | {currentTutorial.title} 152 |
153 |
154 | {" "} 157 | {currentTutorial.description} 158 |
159 |
160 | {" "} 163 | {currentTutorial.published ? "Published" : "Pending"} 164 |
165 | 166 | 170 | Edit 171 | 172 |
173 | ) : ( 174 |
175 |
176 |

Please click on a Tutorial...

177 |
178 | )} 179 |
180 |
181 | ); 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /react-client/src/http-common.js: -------------------------------------------------------------------------------- 1 | import axios from "axios"; 2 | 3 | export default axios.create({ 4 | baseURL: "http://localhost:8080/api", 5 | headers: { 6 | "Content-type": "application/json" 7 | } 8 | }); -------------------------------------------------------------------------------- /react-client/src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { createRoot } from "react-dom/client"; 3 | import { BrowserRouter } from "react-router-dom"; 4 | 5 | import App from "./App"; 6 | import * as serviceWorker from "./serviceWorker"; 7 | 8 | const container = document.getElementById("root"); 9 | const root = createRoot(container); 10 | 11 | root.render( 12 | 13 | 14 | 15 | ); 16 | 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /react-client/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /react-client/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' } 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /react-client/src/services/tutorial.service.js: -------------------------------------------------------------------------------- 1 | import http from "../http-common"; 2 | 3 | class TutorialDataService { 4 | getAll() { 5 | return http.get("/tutorials"); 6 | } 7 | 8 | get(id) { 9 | return http.get(`/tutorials/${id}`); 10 | } 11 | 12 | create(data) { 13 | return http.post("/tutorials", data); 14 | } 15 | 16 | update(id, data) { 17 | return http.put(`/tutorials/${id}`, data); 18 | } 19 | 20 | delete(id) { 21 | return http.delete(`/tutorials/${id}`); 22 | } 23 | 24 | deleteAll() { 25 | return http.delete(`/tutorials`); 26 | } 27 | 28 | findByTitle(title) { 29 | return http.get(`/tutorials?title=${title}`); 30 | } 31 | } 32 | 33 | export default new TutorialDataService(); -------------------------------------------------------------------------------- /react-client/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | --------------------------------------------------------------------------------