Skip to content

Instantly share code, notes, and snippets.

View soonsam123's full-sized avatar

Soon Sam Ribeiro Santos soonsam123

View GitHub Profile
@soonsam123
soonsam123 / controllers-dojos.js
Created January 17, 2021 12:42
NodeJS API-Part 8/ Handling errors
const { Sequelize } = require("sequelize");
const Dojos = require("../models/dojos");
module.exports = {
async getAll(req, res) {
// TODO: This request will result in an error because table name is wrong: `martial_art`...
const sequelize = new Sequelize("martial_art", "root", "password", {
host: "127.0.0.1",
dialect: "mysql",
});
const express = require("express");
+ const bodyParser = require("body-parser");
const dojosRoutes = require("./api/routes/dojos");
const app = express();
+ const jsonParser = bodyParser.json();
app.listen(8000);
@soonsam123
soonsam123 / controllers-dojos.js
Created December 8, 2020 01:52
NodeJS API-Part 7/ CRUD with sequelize and mysql2
const { Sequelize } = require("sequelize");
const Dojos = require("../models/dojos");
module.exports = {
async getAll(req, res) {
const sequelize = new Sequelize("martial_arts", "root", "password", {
host: "127.0.0.1",
dialect: "mysql",
});
const express = require("express");
const dojosController = require("../controllers/dojos");
const router = express.Router();
router.get("/", (req, res) => {
dojosController.getAll(req, res);
});
router.get("/:id", (req, res) => {
@soonsam123
soonsam123 / controllers-dojos.js
Last active December 7, 2020 00:17
NodeJS API-Part 6 / Adding Sequelize and MySQL Database
const { Sequelize } = require("sequelize");
const Dojos = require("../models/dojos");
module.exports = {
async getAll(req, res) {
const sequelize = new Sequelize(
"martial_arts",
"root",
"password",
{
"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Dojos extends Model {}
Dojos.init(
{
dojo_name: DataTypes.STRING,
teacher: DataTypes.STRING,
},
{
CREATE DATABASE martial_arts;
CREATE TABLE dojos (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
dojo_name VARCHAR(255),
teacher VARCHAR(255)
);
INSERT INTO dojos (dojo_name, teacher)
VALUES
const express = require("express");
const dojosRoutes = require("./api/routes/dojos");
const app = express();
app.listen(8000);
/**
* Adding headers to our requests.
*/
const express = require("express");
const dojosController = require("../controllers/dojos");
const router = express.Router();
router.get("/", (req, res) => {
dojosController.getAll(req, res);
});
module.exports = router;
@soonsam123
soonsam123 / controllers-dojos.js
Last active December 6, 2020 22:02
NodeJS API-Part 5 / Model/Router/Controller structure
module.exports = {
getAll(req, res) {
res.send({
id: 1,
name: "KARATÊ DOJO",
teacher: "JACKSON TANAKA",
});
},
};