This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const mysql = require('mysql'); | |
| //- NEW | |
| require('dotenv').config(); | |
| const { DB_HOST, DB_USER, DB_PASS, DB_DATABASE } = process.env; | |
| //- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| HOST=127.0.0.1 | |
| PORT=3000 | |
| DB_HOST=localhost | |
| DB_USER=root | |
| DB_PASS= | |
| DB_DATABASE=nodejs_mysql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE DATABASE nodejs_mysql; | |
| USE nodejs_mysql; | |
| CREATE TABLE documents ( | |
| id int(11) NOT NULL AUTO_INCREMENT, | |
| title varchar(50) NOT NULL, | |
| description text NULL, | |
| created_at datetime NOT NULL, | |
| updated_at datetime NOT NULL, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const Document = require('@models/document.model'); | |
| const { documentHasAllData } = require('@validators/document.validator'); | |
| exports.getAll = (request, response) => { | |
| const callback = (err, documents) => (err) | |
| ? response.send(err) | |
| : response.send({ documents }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const router = express.Router(); | |
| const documentController = require('@controllers/document.controller'); | |
| router.get('/', documentController.getAll); | |
| router.get('/:id', documentController.getById); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const db = require('@config/db'); | |
| let Document = function (document) { | |
| this.id = document.id; | |
| this.title = document.title; | |
| this.description = document.description; | |
| this.created_at = document.created_at ?? new Date(); | |
| this.updated_at = new Date(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| const mysql = require('mysql2'); | |
| const dbConnection = mysql.createConnection({ | |
| host : 'localhost', | |
| user : 'root', | |
| password : '', | |
| database : 'nodejs_mysql' | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const app = express(); | |
| const host = '127.0.0.1'; | |
| const port = 3000; | |
| app.use(express.urlencoded({ extended: true })); | |
| app.use(express.json()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "nodejs_express_mysql", | |
| "version": "1.0.0", | |
| "description": "NodeJS Express Mysql JS Backend", | |
| "main": "server.js", | |
| "scripts": { | |
| "start": "nodemon server.js" | |
| }, | |
| "keywords": [ | |
| "NodeJS", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const http = require('http'); | |
| const hostname = 'localhost'; | |
| const port = 3000; | |
| const server = http.createServer((req, res) => { | |
| res.statusCode = 200; | |
| res.setHeader('Content-Type', 'text/plain'); | |
| res.end('NodeJS works in Nginx!\n'); | |
| }); |
NewerOlder