Mission4
.localhost:5000/api/items
localhost:5000/api/items
POST
requestraw
JSON
{ "name": "test", "status": true }
.gitignore
file into this folder.myNotes.txt
file in this folder.cd mission4 ng new client
cd client npm i bootstrap
In style.css
write
@import "bootstrap/dist/css/bootstrap.min.css";
Update angular.json
file.
"styles": [ "src/styles.css", "./node_modules/bootstrap/dist/css/bootstrap.min.css" ],
proxy.conf.json
file under client folder with:
{ "/api/*": { "target": "http://localhost:5000", "secure": false } }
angular.json
file.
... "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "browserTarget": "client:build", "proxyConfig": "proxy.conf.json" <-------- Add this line only!!! },
Create server.js
file.
npm init -y
npm i express mysql
npm i -D nodemon concurrently
Update package.json
file
"scripts": { "start": "node server.js", "server": "nodemon server.js --ignore ./client", "client": "npm start --prefix client" },
CREATE DATABASE mission4;
CREATE USER 'johnBryce_user'@'localhost' IDENTIFIED BY 'myPassword';
SELECT * FROM mysql.user;
GRANT ALL PRIVILEGES ON mission4.* TO 'johnBryce_user'@'localhost';
FLUSH PRIVILEGES;
CREATE TABLE IF NOT EXISTS mission4.companies ( id INT AUTO_INCREMENT PRIMARY KEY, companyName CHAR(100) NOT NULL );
CREATE TABLE mission4.servers ( id INT AUTO_INCREMENT PRIMARY KEY, serverName CHAR(100) NOT NULL, ip CHAR(255) NOT NULL, hostCompId INT NOT NULL, status BOOLEAN DEFAULT FALSE, createdAt TIMESTAMP DEFAULT NOW(), FOREIGN KEY (hostCompId) REFERENCES mission4.companies(id) );
CREATE TABLE IF NOT EXISTS mission4.products ( id INT AUTO_INCREMENT PRIMARY KEY, name CHAR(100) NOT NULL, price INT NOT NULL, vendorId INT NOT NULL, units INT NOT NULL DEFAULT 0, updatedAt TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (vendorId) REFERENCES mission4.vendors(id) );
INSERT INTO mission4.companies (companyName) VALUES ('IBM'), ('Microsoft'), ('Google'), ('DigitalO');
INSERT INTO mission4.servers (serverName, ip, hostCompId) VALUES ('Server A', '1.1.1.1', 1), ('Server B', '2.2.2.2', 2), ('Server C', '3.3.3.3', 3), ('Server D', '4.4.4.4', 4);
INSERT INTO mission4.servers (serverName, ip, hostCompId, status, createdAt) VALUES ('Server F', '10.116.093.192', 3, 1, "2010-01-31 19:00:00"), ('Server G', '10.010.093.102', 4, 0, "2016-02-29 18:00:00"), ('Server H', '10.100.100.100', 3, 1, "2019-03-31 20:00:00"), ('Server T', '10.170.193.132', 1, 0, "2018-04-30 19:30:00");
SELECT s.id, s.serverName, s.ip, s.hostCompId, s.status, s.createdAt, c.id, c.companyName FROM mission4.servers AS s LEFT JOIN mission4.companies AS c ON s.hostCompId = c.id
See above
UPDATE mission4.servers SET status = true WHERE id = 1
DELETE FROM mission4.tasks WHERE id = 7;
const mysql = require('mysql'); const pool = mysql.createPool({ connectionLimit: 10, host: 'localhost', user: 'johnBryce_user', password: 'myPassword', database: 'mission4' }); pool.on('connection', () => console.log('Mysql connection')); /* pool.query('SELECT * FROM servers', (err, res) => { if (err) throw err; console.log(res); }); */ module.exports = { pool };
const express = require('express'); const { pool } = require('./database/db'); const app = express(); app.use(express.json()); // GET app.get('/api/items', (req, res) => { res.status(200).json({}); }); // POST app.post('/api/items', (req, res) => { res.status(200).json({}); }); const PORT = process.env.PORT || 5000; app.listen(PORT, console.log(`Server started on port ${PORT}`));
// GET app.get('/api/servers', (req, res) => { const sql = ` SELECT s.id, s.serverName, s.ip, s.hostCompId, s.status, s.createdAt, c.id, c.companyName FROM servers AS s LEFT JOIN companies AS c ON s.hostCompId = c.id `; pool.query(sql, (err, results) => { if (err) { res.status(500).json({success: false}); throw err; } res.status(200).json({success: true, data: results}); }); });
// POST app.post('/api/todolist', (req, res) => { const { taskName, ownerId } = req.body; //Validation if (isNaN(Number(ownerId)) || !taskName) { return res.status(400).json({success: false}); } const newTask = { taskName, ownerId }; const sql = ` INSERT INTO tasks SET ? `; pool.query(sql, [newTask], (err, results, fields) => { if (err) { res.status(400).json({success: false}); throw err; } res.status(200).json({success: true}); }) });
// PUT app.put('/api/server/status', (req, res) => { const { status, id } = req.body; // Validation if (isNaN(Number(id)) || ![true, false].includes(status)) { console.log('Validation has failed.'); return res.status(400).json({success: false}); } const sql = ` UPDATE servers SET status = ? WHERE id = ? `; pool.query(sql, [status, id], (err, results) => { if (err) { res.status(500).json({success: false}); throw err; } res.status(200).json({success: true}); }); });
//DELETE app.delete('/api/todolist/:id', (req, res) => { const taskId = req.params.id; //Validation if (isNaN(Number(taskId))) { return res.status(400).json({success: false}); } const sql = ` DELETE FROM tasks WHERE id = ?; `; pool.query(sql, [taskId], (err, results, fields) => { if (err) { res.status(400).json({success: false}); throw err; } res.status(200).json({success: true}); }); });