Skip to content

Commit be702f0

Browse files
committed
update for working with frontend
1 parent d8cef0f commit be702f0

File tree

10 files changed

+339
-271
lines changed

10 files changed

+339
-271
lines changed

README.md

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,56 @@
11
# Node.js Rest APIs with Express & MySQL example
22

33
For instruction, please visit:
4-
> [Build Node.js Rest APIs with Express & MySQL](https://bezkoder.com/node-js-rest-api-express-mysql/)
4+
> [Build Node.js Rest APIs with Express & MySQL](https://www.bezkoder.com/node-js-rest-api-express-mysql/)
5+
6+
Front-end that works well with this Back-end
7+
> [Axios Client](https://www.bezkoder.com/axios-request/)
8+
9+
> [Angular 8 Client](https://www.bezkoder.com/angular-crud-app/) / [Angular 10 Client](https://www.bezkoder.com/angular-10-crud-app/) / [Angular 11 Client](https://www.bezkoder.com/angular-11-crud-app/) / [Angular 12 Client](https://www.bezkoder.com/angular-12-crud-app/) / [Angular 13 Client](https://www.bezkoder.com/angular-13-crud-example/)
10+
11+
> [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/)
12+
13+
> [React Client](https://www.bezkoder.com/react-crud-web-api/) / [React Redux Client](https://www.bezkoder.com/react-redux-crud-example/)
514
615
More Practice
7-
> [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/)
16+
> [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://www.bezkoder.com/node-js-express-sequelize-mysql/)
17+
18+
> [Server side Pagination in Node.js with Sequelize and MySQL](https://www.bezkoder.com/node-js-sequelize-pagination-mysql/)
819
9-
> [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/)
20+
> [Node.js Express File Upload Rest API example](https://www.bezkoder.com/node-js-express-file-upload/)
1021
11-
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
22+
> [Node.js Express File Upload with Google Cloud Storage example](https://www.bezkoder.com/google-cloud-storage-nodejs-upload-file/)
23+
24+
> [Node.js: Upload CSV file data into Database with Express](https://www.bezkoder.com/node-js-upload-csv-file-database/)
25+
26+
> [Node.js: Upload Excel file data into Database with Express](https://www.bezkoder.com/node-js-upload-excel-file-database/)
27+
28+
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
1229
1330
Security:
14-
> [Node.js Express: JWT example | Token Based Authentication & Authorization](https://bezkoder.com/node-js-jwt-authentication-mysql/)
31+
> [Node.js Express: JWT example | Token Based Authentication & Authorization](https://www.bezkoder.com/node-js-jwt-authentication-mysql/)
1532
1633
Associations:
17-
> [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/)
34+
> [Sequelize Associations: One-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-one-to-many/)
1835
19-
> [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/)
36+
> [Sequelize Associations: Many-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-many-to-many/)
2037
2138
Fullstack:
22-
> [Vue.js + Node.js + Express + MySQL example](https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/)
39+
> [Vue.js + Node.js + Express + MySQL example](https://www.bezkoder.com/vue-js-node-js-express-mysql-crud-example/)
40+
41+
> [Vue.js + Node.js + Express + MongoDB example](https://www.bezkoder.com/vue-node-express-mongodb-mevn-crud/)
42+
43+
> [Angular 8 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-node-express-mysql/)
2344
24-
> [Vue.js + Node.js + Express + MongoDB example](https://bezkoder.com/vue-node-express-mongodb-mevn-crud/)
45+
> [Angular 10 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-10-node-js-express-mysql/)
2546
26-
> [Angular 8 + Node.js + Express + MySQL example](https://bezkoder.com/angular-node-express-mysql/)
47+
> [Angular 11 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-11-node-js-express-mysql/)
2748
28-
> [Angular 10 + Node.js + Express + MySQL example](https://bezkoder.com/angular-10-node-js-express-mysql/)
49+
> [Angular 12 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-12-node-js-express-mysql/)
2950
30-
> [Angular 11 + Node.js Express + MySQL example](https://bezkoder.com/angular-11-node-js-express-mysql/)
51+
> [React + Node.js + Express + MySQL example](https://www.bezkoder.com/react-node-express-mysql/)
3152
32-
> [React + Node.js + Express + MySQL example](https://bezkoder.com/react-node-express-mysql/)
53+
> [React + Redux + Node.js Express + MySQL](https://www.bezkoder.com/react-redux-mysql-crud/)
3354
3455
## Project setup
3556
```

app/config/db.config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
2-
HOST: "us-cdbr-iron-east-02.cleardb.net",
3-
USER: "b7e24378878xxx",
4-
PASSWORD: "0200exxx",
5-
DB: "heroku_7643ec736354xxx"
6-
};
2+
HOST: "localhost",
3+
USER: "root",
4+
PASSWORD: "123456",
5+
DB: "testdb"
6+
};

app/controllers/customer.controller.js

Lines changed: 0 additions & 116 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const Tutorial = require("../models/tutorial.model.js");
2+
3+
// Create and Save a new Tutorial
4+
exports.create = (req, res) => {
5+
// Validate request
6+
if (!req.body) {
7+
res.status(400).send({
8+
message: "Content can not be empty!"
9+
});
10+
}
11+
12+
// Create a Tutorial
13+
const tutorial = new Tutorial({
14+
title: req.body.title,
15+
description: req.body.description,
16+
published: req.body.published || false
17+
});
18+
19+
// Save Tutorial in the database
20+
Tutorial.create(tutorial, (err, data) => {
21+
if (err)
22+
res.status(500).send({
23+
message:
24+
err.message || "Some error occurred while creating the Tutorial."
25+
});
26+
else res.send(data);
27+
});
28+
};
29+
30+
// Retrieve all Tutorials from the database (with condition).
31+
exports.findAll = (req, res) => {
32+
const title = req.query.title;
33+
34+
Tutorial.getAll(title, (err, data) => {
35+
if (err)
36+
res.status(500).send({
37+
message:
38+
err.message || "Some error occurred while retrieving tutorials."
39+
});
40+
else res.send(data);
41+
});
42+
};
43+
44+
// Find a single Tutorial by Id
45+
exports.findOne = (req, res) => {
46+
Tutorial.findById(req.params.id, (err, data) => {
47+
if (err) {
48+
if (err.kind === "not_found") {
49+
res.status(404).send({
50+
message: `Not found Tutorial with id ${req.params.id}.`
51+
});
52+
} else {
53+
res.status(500).send({
54+
message: "Error retrieving Tutorial with id " + req.params.id
55+
});
56+
}
57+
} else res.send(data);
58+
});
59+
};
60+
61+
// find all published Tutorials
62+
exports.findAllPublished = (req, res) => {
63+
Tutorial.getAllPublished((err, data) => {
64+
if (err)
65+
res.status(500).send({
66+
message:
67+
err.message || "Some error occurred while retrieving tutorials."
68+
});
69+
else res.send(data);
70+
});
71+
};
72+
73+
// Update a Tutorial identified by the id in the request
74+
exports.update = (req, res) => {
75+
// Validate Request
76+
if (!req.body) {
77+
res.status(400).send({
78+
message: "Content can not be empty!"
79+
});
80+
}
81+
82+
console.log(req.body);
83+
84+
Tutorial.updateById(
85+
req.params.id,
86+
new Tutorial(req.body),
87+
(err, data) => {
88+
if (err) {
89+
if (err.kind === "not_found") {
90+
res.status(404).send({
91+
message: `Not found Tutorial with id ${req.params.id}.`
92+
});
93+
} else {
94+
res.status(500).send({
95+
message: "Error updating Tutorial with id " + req.params.id
96+
});
97+
}
98+
} else res.send(data);
99+
}
100+
);
101+
};
102+
103+
// Delete a Tutorial with the specified id in the request
104+
exports.delete = (req, res) => {
105+
Tutorial.remove(req.params.id, (err, data) => {
106+
if (err) {
107+
if (err.kind === "not_found") {
108+
res.status(404).send({
109+
message: `Not found Tutorial with id ${req.params.id}.`
110+
});
111+
} else {
112+
res.status(500).send({
113+
message: "Could not delete Tutorial with id " + req.params.id
114+
});
115+
}
116+
} else res.send({ message: `Tutorial was deleted successfully!` });
117+
});
118+
};
119+
120+
// Delete all Tutorials from the database.
121+
exports.deleteAll = (req, res) => {
122+
Tutorial.removeAll((err, data) => {
123+
if (err)
124+
res.status(500).send({
125+
message:
126+
err.message || "Some error occurred while removing all tutorials."
127+
});
128+
else res.send({ message: `All Tutorials were deleted successfully!` });
129+
});
130+
};

0 commit comments

Comments
 (0)