0% found this document useful (0 votes)
202 views

Node Js

This document contains code for a Node.js/Express/MySQL web app that performs CRUD operations on a database table. It includes code to connect to a MySQL database, define routes for GET, POST, and PUT requests, and write/read data to/from the database table. Comments indicate routes are defined but not fully implemented for delete and update operations. The app renders EJS templates and uses Bootstrap for styling.

Uploaded by

preda andrei
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

Node Js

This document contains code for a Node.js/Express/MySQL web app that performs CRUD operations on a database table. It includes code to connect to a MySQL database, define routes for GET, POST, and PUT requests, and write/read data to/from the database table. Comments indicate routes are defined but not fully implemented for delete and update operations. The app renders EJS templates and uses Bootstrap for styling.

Uploaded by

preda andrei
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

index.

ejs
link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></
script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

http://learninfinity.info/crud-operation-using-nodejs-expressjs-mysql-part-2/
http://learninfinity.info/crud-operation-using-nodejs-expressjs-mysql-part-1/

app.post("/add", (req, res) => {


res.render('add.ejs', {
title: 'Date adaugate!'
});
});
app.post('/save',(req, res) => {
const data = { NumarDosarPenal = body.Numar_Dosar_Penal, DataScriereTrafic =
req.body.Data_Scriere_Trafic, DataInceput = req.body.Data_Inceput, OraIncepu =
req.body.Ora_Inceput, DataFinal = req.body.Data_Final, OraFinal =
req.body.Ora_Final, Marca = req.body.Marca, adauga = req.body.Coloana};
const sql = "INSERT INTO users SET ?";
const query = connection.query(sql, data,(err, results) => {
if(err) throw err;
res.redirect('/');
});
});

conectare.js 11.12.2020 14-00

const path = require("path");


const express = require("express");
const ejs = require("ejs");
const mysql = require("mysql");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;

//set path to style filles


app.use(express.static("../stehnic"));

//set view file


app.set("views", path.join(__dirname, "views"));

//set view engine


app.set("view egine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

//set conection to mysql db


const connection = mysql.createConnection({
host: "localhost",
user: "PredaA",
password: "hyY4GhJepVGPDk6n",
database: "stehnic",
});

connection.connect(function (err) {
if (err) throw err;
console.log("Connnected..");
});

//show my form html page


app.get("/", (req, res) => {
let sql = "SELECT * FROM trafic";
let query = connection.query(sql, (err, rows) => {
if (err) throw err;
res.render("trafic", {
title: "WEB APP",
user: rows,
});
});
});

//end show

app.post("/submit", function (req, res) {


const sql =
"insert into trafic ( NumarDosarPenal, DataScriereTrafic, DataInceput,
OraInceput, DataFinal, OraFinal, Marca, adauga ) values ('" +
req.body.Numar_Dosar_Penal +
"', '" +
req.body.Data_Scriere_Trafic +
"','" +
req.body.Data_Inceput +
"','" +
req.body.Ora_Inceput +
"','" +
req.body.Data_Final +
"','" +
req.body.Ora_Final +
"','" +
req.body.Marca +
"','" +
req.body.Coloana +
"')";

connection.query(sql, function (err, result) {


if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
res.render("../stehnic/trafic", {
title: "Data Saved",
message: "Data Saved successfully",
});
});
//connection.end();
});

/*
//Delete
app.delete("/trafic/:id", (req, res) => {
mysqlConnection.query(
"DELETE FROM trafic WHERE ID = ?",
[req.params.id],
(err, rows, fields) => {
if (!err) res.send("Deleted successfully.");
else console.log(err);
}
);
});

//Update
app.put("/trafic", (req, res) => {
const tfr = req.body;
const sql =
"SET @NumarDosarPenal = ?; SET @DataScriereTrafic = ?; SET @DataInceput = ?;
SET @OraInceput = ?; SET @DataFinal = ?;SET @OraFinal = ?; SET @Marca = ?;SET
@Coloana = ?; \
CALL traficEdit(@Numar_Dosar_Penal, @Data_Scriere_Trafic, @Data_Inceput,
@Ora_Inceput, @Data_Final, @Ora_Final, @Marca, @Coloana);";
mysqlConnection.query(
sql,
[
tfr.Numar_Dosar_Penal,
tfr.Data_Scriere_Trafic,
tfr.Data_Inceput,
tfr.Ora_Inceput,
tfr.Data_Final,
tfr.Ora_Final,
tfr.Marca,
tfr.Coloana,
],
(err, rows, fields) => {
if (!err) res.send("Updated successfully");
else console.log(err);
}
);
});
*/

app.listen(port, function () {
console.log(`Listening on port ${port}`);
});

You might also like