Skip to content

H4ck3rBatera/NodeJS.MySql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS.MysSql

NodeJS + MysSql

Download the Node.js

VSCode Extensions | Recommendations

{
    "recommendations": [
        "codezombiech.gitignore",
        "ms-azuretools.vscode-docker",
        "cweijan.vscode-mysql-client2"
    ]
}

Shell

npm -v

npm init

npm install express -save

npm install ejs --save

npm install -g nodemon

npm install mysql --save

nodemon app

Docker | Yaml

version: '3'

services:

  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: "Fibonacci"
      MYSQL_DATABASE: "Node"
    ports:
      - 3306:3306

Sql

CREATE TABLE Technology(  
    Id int NOT NULL primary key AUTO_INCREMENT,
    Name varchar(50)
) default charset utf8 comment '';

INSERT INTO Technology(Name) VALUES('Drones')

DbConnection

const mysql = require('mysql2/promise');

module.exports = async function () {
    return await mysql.createConnection("mysql://root:Fibonacci@localhost:3306/Node");
};

Technology Repository

const dbConnection = require('../config/dbConnection');

async function list() {
    const connection = await dbConnection();

    const [rows] = await connection.query("SELECT Id, Name FROM Technology;");
    return rows;
}

module.exports = { list }

Technology Route

const repository = require('../../data/technologyRepository');

module.exports = function (app) {
    app.get('/technology', async function (req, res) {
        const list = await repository.list();

        res.render("technology/listView", { list: list });
    });
}

List View

<html>

<body>
    <table>
        <tr>
            <td>
                ID
            </td>
            <td>
                Name
            </td>
        </tr>

        <% for(var i=0; i < list.length; i++) {%>
            <tr>
                <td>
                    <%= list[i].Id %>
                </td>
                <td>
                    <%= list[i].Name %>
                </td>
            </tr>
            <% } %>

    </table>
</body>

</html>