Build A Simple App Using Node JS and MySQL.
Build A Simple App Using Node JS and MySQL.
pdf
Guardado en Dropbox • 24-06-2018 09F11
search 1
#node #mysql
Prerequisites
Before you join this tutorial it is assumed that
you meet the requirements listed below:
Folder Structure
This is how the project will be structured.
mkdir node-mysql-crud-app
cd node-mysql-crud-app
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable
=no, initial-scale=1.0, maximum-scale=1.0, minimum-s
cale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=e
dge">
<link rel="stylesheet" href="https://maxcdn.boot
strapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTN
h0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymo
us">
<!--<link rel="stylesheet" href="/assets/css/cus
tom.css">-->
<title><%= title %></title>
</head>
<style>
.table-wrapper {
margin-top: 50px;
}
.player-img {
width: 40px;
height: 40px;
}
.add-player-form {
margin-top: 50px;
}
</style>
<body>
<div class="page-wrapper">
<nav class="navbar navbar-light bg-light">
<span class="navbar-brand mb-0 h1" ><a href=
"/">Socka Players</a></span>
<a class="float-right" href="/add" title="Ad
d a New Player">Add a Player</a>
index.ejs
</nav>
add-player.ejs
Go <a href="/add"
<% } %>
>here</a> to add players.</p>
</div>
</div>
This page
</body> contains the form to add a new
player to the database.
</html>
edit-player.ejs
</div>
</body>
</html>
App.js</form>
<% } else { %>
<p class="text-center">Player Not Found. Go
<a href="/add">here</a> to add players.</p>
const express = require('express');
<% } %>
const fileUpload = require('express-fileupload');
</div>
const bodyParser = require('body-parser');
</div>
const mysql = require('mysql');
</body>
const path = require('path');
</html>
const app = express();
// connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
// configure middleware
app.set('port', process.env.port || port); // set ex
press to use this port
app.set('views', __dirname + '/views'); // set expre
ss to look in this folder to render our view
app.set('view engine', 'ejs'); // configure template
engine
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data clien
t
app.use(express.static(path.join(__dirname, 'public'
))); // configure express to use public folder
app.use(fileUpload()); // configure fileupload
nodemon app.js
// execute query
db.query(query, (err, result) => {
if (err) {
res.redirect('/');
}
res.render('index.ejs', {
title: "Welcome to Socka | View Play
ers"
,players: result
});
});
},
};
player.js
module.exports = {
addPlayerPage: (req, res) => {
res.render('add-player.ejs', {
title: "Welcome to Socka | Add a new pla
yer"
,message: ''
});
},
addPlayer: (req, res) => {
if (!req.files) {
return res.status(400).send("No files we
re uploaded.");
}
fs.unlink(`public/assets/img/${image}`,
(err) => {
if (err) {
return res.status(500).send(err)
;
}
db.query(deleteUserQuery, (err, resu
lt) => {
if (err) {
return res.status(500).send(
err);
}
res.redirect('/');
});
});
This file
});handles all the post and get requests
} the players page. The add player function
for
};
contains a function that uploads the player's
image to the /public/assets/img directory and
sends the player's details to the database.
on line 8 and 9
// const {getHomePage} = require('./routes/index');
// const {addPlayerPage, addPlayer, deletePlayer, ed
itPlayer, editPlayerPage} = require('./routes/player
');
on line 40 - 47
/*
app.get('/', getHomePage);
app.get('/add', addPlayerPage);
app.get('/edit/:id', editPlayerPage);
app.get('/delete/:id', deletePlayer);
app.post('/add', addPlayer);
app.post('/edit/:id', editPlayer);
*/
5 2 1
dev.to is where software developers stay in
the loop and avoid career stagnation.
PREVIEW SUBMIT
What is GraphQL
Grigor Khachatryan
Janeth - Jun 21
copyright 2018