Skip to content

Commit 8581918

Browse files
chore: setup
1 parent 03bdae5 commit 8581918

File tree

17 files changed

+1864
-0
lines changed

17 files changed

+1864
-0
lines changed

app/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.env

app/controllers/data.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fetchData = async (req,res) => {
2+
res.status(200).send({msg:"success"});
3+
}
4+
module.exports = {fetchData}

app/db/dbConnect.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const mysql = require('mysql2');
2+
require('dotenv').config();
3+
// Create a connection to the MySQL server
4+
const connection = mysql.createConnection({
5+
host: process.env.dbHost, // Change this to your MySQL server's host
6+
user: process.env.dbUser, // Change this to your MySQL username
7+
password: process.env.dbPass, // Change this to your MySQL password
8+
database: process.env.dbName // Change this to your MySQL database name
9+
});
10+
connection.connect((err) => {
11+
if (err) {
12+
console.error('Error connecting to MySQL server: ' + err.stack);
13+
return;
14+
}
15+
16+
console.log('Connected to MySQL server as ID ' + connection.threadId);
17+
});
18+
// db.query('SELECT * FROM sys_config', (error, results, fields) => {
19+
// if (error) {
20+
// console.error('Error executing query: ' + error);
21+
// return;
22+
// }
23+
// console.log('Query result: ', results);
24+
// });
25+
26+
module.exports = connection;

app/main.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require('express')
2+
const app = express()
3+
app.set("view engine", 'ejs');
4+
app.use(express.static("public"))
5+
require('dotenv').config();
6+
const api = require('./routes/data')
7+
const auth = require('./routes/auth')
8+
const db = require('./db/dbConnect')
9+
const bodyParser = require('body-parser');
10+
app.use(bodyParser.urlencoded({ extended: true }));
11+
app.use(bodyParser.json());
12+
13+
app.use("/api/data", api);
14+
app.use("/auth",auth);
15+
16+
app.post('/search', (req, res) => {
17+
db.query('SELECT * FROM products WHERE name LIKE \'\%'+req.body.name+'\%\';', (error, results, fields) => {
18+
if (error) {
19+
console.error('Error executing query: ' + error);
20+
return;
21+
}
22+
console.log(results)
23+
res.render('products',{products:results});
24+
});
25+
})
26+
27+
app.get('/', (req,res) => {
28+
db.query('SELECT * FROM products', (error, results, fields) => {
29+
if (error) {
30+
console.error('Error executing query: ' + error);
31+
return;
32+
}
33+
res.render('products',{products:results});
34+
});
35+
})
36+
37+
app.listen(4444);

0 commit comments

Comments
 (0)