Skip to content

Commit

Permalink
update for working with frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
tienbku committed Nov 10, 2021
1 parent d8cef0f commit be702f0
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 271 deletions.
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,56 @@
# Node.js Rest APIs with Express & MySQL example

For instruction, please visit:
> [Build Node.js Rest APIs with Express & MySQL](https://bezkoder.com/node-js-rest-api-express-mysql/)
> [Build Node.js Rest APIs with Express & MySQL](https://www.bezkoder.com/node-js-rest-api-express-mysql/)
Front-end that works well with this Back-end
> [Axios Client](https://www.bezkoder.com/axios-request/)
> [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/)
> [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/)
> [React Client](https://www.bezkoder.com/react-crud-web-api/) / [React Redux Client](https://www.bezkoder.com/react-redux-crud-example/)
More Practice
> [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://bezkoder.com/node-js-express-sequelize-mysql/)
> [Build Node.js Rest APIs with Express, Sequelize & MySQL](https://www.bezkoder.com/node-js-express-sequelize-mysql/)
> [Server side Pagination in Node.js with Sequelize and MySQL](https://www.bezkoder.com/node-js-sequelize-pagination-mysql/)
> [Server side Pagination in Node.js with Sequelize and MySQL](https://bezkoder.com/node-js-sequelize-pagination-mysql/)
> [Node.js Express File Upload Rest API example](https://www.bezkoder.com/node-js-express-file-upload/)
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
> [Node.js Express File Upload with Google Cloud Storage example](https://www.bezkoder.com/google-cloud-storage-nodejs-upload-file/)
> [Node.js: Upload CSV file data into Database with Express](https://www.bezkoder.com/node-js-upload-csv-file-database/)
> [Node.js: Upload Excel file data into Database with Express](https://www.bezkoder.com/node-js-upload-excel-file-database/)
> [Deploying/Hosting Node.js app on Heroku with MySQL database](https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/)
Security:
> [Node.js Express: JWT example | Token Based Authentication & Authorization](https://bezkoder.com/node-js-jwt-authentication-mysql/)
> [Node.js Express: JWT example | Token Based Authentication & Authorization](https://www.bezkoder.com/node-js-jwt-authentication-mysql/)
Associations:
> [Sequelize Associations: One-to-Many Relationship example](https://bezkoder.com/sequelize-associate-one-to-many/)
> [Sequelize Associations: One-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-one-to-many/)
> [Sequelize Associations: Many-to-Many Relationship example](https://bezkoder.com/sequelize-associate-many-to-many/)
> [Sequelize Associations: Many-to-Many Relationship example](https://www.bezkoder.com/sequelize-associate-many-to-many/)
Fullstack:
> [Vue.js + Node.js + Express + MySQL example](https://bezkoder.com/vue-js-node-js-express-mysql-crud-example/)
> [Vue.js + Node.js + Express + MySQL example](https://www.bezkoder.com/vue-js-node-js-express-mysql-crud-example/)
> [Vue.js + Node.js + Express + MongoDB example](https://www.bezkoder.com/vue-node-express-mongodb-mevn-crud/)
> [Angular 8 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-node-express-mysql/)
> [Vue.js + Node.js + Express + MongoDB example](https://bezkoder.com/vue-node-express-mongodb-mevn-crud/)
> [Angular 10 + Node.js + Express + MySQL example](https://www.bezkoder.com/angular-10-node-js-express-mysql/)
> [Angular 8 + Node.js + Express + MySQL example](https://bezkoder.com/angular-node-express-mysql/)
> [Angular 11 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-11-node-js-express-mysql/)
> [Angular 10 + Node.js + Express + MySQL example](https://bezkoder.com/angular-10-node-js-express-mysql/)
> [Angular 12 + Node.js Express + MySQL example](https://www.bezkoder.com/angular-12-node-js-express-mysql/)
> [Angular 11 + Node.js Express + MySQL example](https://bezkoder.com/angular-11-node-js-express-mysql/)
> [React + Node.js + Express + MySQL example](https://www.bezkoder.com/react-node-express-mysql/)
> [React + Node.js + Express + MySQL example](https://bezkoder.com/react-node-express-mysql/)
> [React + Redux + Node.js Express + MySQL](https://www.bezkoder.com/react-redux-mysql-crud/)
## Project setup
```
Expand Down
10 changes: 5 additions & 5 deletions app/config/db.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
HOST: "us-cdbr-iron-east-02.cleardb.net",
USER: "b7e24378878xxx",
PASSWORD: "0200exxx",
DB: "heroku_7643ec736354xxx"
};
HOST: "localhost",
USER: "root",
PASSWORD: "123456",
DB: "testdb"
};
116 changes: 0 additions & 116 deletions app/controllers/customer.controller.js

This file was deleted.

130 changes: 130 additions & 0 deletions app/controllers/tutorial.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const Tutorial = require("../models/tutorial.model.js");

// Create and Save a new Tutorial
exports.create = (req, res) => {
// Validate request
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}

// Create a Tutorial
const tutorial = new Tutorial({
title: req.body.title,
description: req.body.description,
published: req.body.published || false
});

// Save Tutorial in the database
Tutorial.create(tutorial, (err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while creating the Tutorial."
});
else res.send(data);
});
};

// Retrieve all Tutorials from the database (with condition).
exports.findAll = (req, res) => {
const title = req.query.title;

Tutorial.getAll(title, (err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while retrieving tutorials."
});
else res.send(data);
});
};

// Find a single Tutorial by Id
exports.findOne = (req, res) => {
Tutorial.findById(req.params.id, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
message: `Not found Tutorial with id ${req.params.id}.`
});
} else {
res.status(500).send({
message: "Error retrieving Tutorial with id " + req.params.id
});
}
} else res.send(data);
});
};

// find all published Tutorials
exports.findAllPublished = (req, res) => {
Tutorial.getAllPublished((err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while retrieving tutorials."
});
else res.send(data);
});
};

// Update a Tutorial identified by the id in the request
exports.update = (req, res) => {
// Validate Request
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}

console.log(req.body);

Tutorial.updateById(
req.params.id,
new Tutorial(req.body),
(err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
message: `Not found Tutorial with id ${req.params.id}.`
});
} else {
res.status(500).send({
message: "Error updating Tutorial with id " + req.params.id
});
}
} else res.send(data);
}
);
};

// Delete a Tutorial with the specified id in the request
exports.delete = (req, res) => {
Tutorial.remove(req.params.id, (err, data) => {
if (err) {
if (err.kind === "not_found") {
res.status(404).send({
message: `Not found Tutorial with id ${req.params.id}.`
});
} else {
res.status(500).send({
message: "Could not delete Tutorial with id " + req.params.id
});
}
} else res.send({ message: `Tutorial was deleted successfully!` });
});
};

// Delete all Tutorials from the database.
exports.deleteAll = (req, res) => {
Tutorial.removeAll((err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Some error occurred while removing all tutorials."
});
else res.send({ message: `All Tutorials were deleted successfully!` });
});
};
Loading

0 comments on commit be702f0

Please sign in to comment.