Skip to content

Commit d68529a

Browse files
author
godsamix
committed
first commit
0 parents  commit d68529a

File tree

10 files changed

+1673
-0
lines changed

10 files changed

+1673
-0
lines changed

.gitignore

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# Snowpack dependency directory (https://snowpack.dev/)
45+
web_modules/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
.parcel-cache
78+
79+
# Next.js build output
80+
.next
81+
out
82+
83+
# Nuxt.js build / generate output
84+
.nuxt
85+
dist
86+
87+
# Gatsby files
88+
.cache/
89+
# Comment in the public line in if your project uses Gatsby and not Next.js
90+
# https://nextjs.org/blog/next-9-1#public-directory-support
91+
# public
92+
93+
# vuepress build output
94+
.vuepress/dist
95+
96+
# Serverless directories
97+
.serverless/
98+
99+
# FuseBox cache
100+
.fusebox/
101+
102+
# DynamoDB Local files
103+
.dynamodb/
104+
105+
# TernJS port file
106+
.tern-port
107+
108+
# Stores VSCode versions used for testing VSCode extensions
109+
.vscode-test
110+
111+
# yarn v2
112+
.yarn/cache
113+
.yarn/unplugged
114+
.yarn/build-state.yml
115+
.yarn/install-state.gz
116+
.pnp.*

Controller/UserController.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var express = require('express');
2+
var router = express.Router();
3+
var sql = require("mysql");
4+
var app = express();
5+
var conn = require("../connection/connect")
6+
7+
//show all Users
8+
router.get('/api/Users',(req, res) => {
9+
let sql = "SELECT * FROM users";
10+
let query = conn.query(sql, (err, results) => {
11+
if(err) throw err;
12+
res.send(JSON.stringify(results));
13+
});
14+
});
15+
16+
//add new User
17+
router.post('/api/User',(req, res) => {
18+
let data = {name: req.query.name, country: req.query.country, number: req.query.number};
19+
let sql = "INSERT INTO users SET ?";
20+
let query = conn.query(sql, data,(err, results) => {
21+
if(err) throw err;
22+
res.send(JSON.stringify(results));
23+
});
24+
});
25+
26+
//update User
27+
router.put('/api/User/:id',(req, res) => {
28+
let sql = "UPDATE users SET name='"+req.query.name+"', country='"+req.query.country+"', number='"+req.query.number+"' WHERE id="+req.params.id;
29+
let query = conn.query(sql, (err, results) => {
30+
// console.log(req.query)
31+
if(err) throw err;
32+
res.send(JSON.stringify(results));
33+
});
34+
});
35+
36+
//Delete User
37+
router.delete('/api/User/:id',(req, res) => {
38+
let sql = "DELETE FROM users WHERE id="+req.params.id;
39+
let query = conn.query(sql, (err, results) => {
40+
if(err) throw err;
41+
res.send(JSON.stringify(results));
42+
});
43+
});
44+
45+
//show single User
46+
router.get('/api/User/:id',(req, res) => {
47+
let sql = "SELECT * FROM users WHERE id="+req.params.id;
48+
let query = conn.query(sql, (err, results) => {
49+
if(err) throw err;
50+
res.send(JSON.stringify(results));
51+
});
52+
});
53+
54+
module.exports = router;

README.md

1.02 KB

Nodejs-mysql-api-example

This is an example of building REST api with MySQL integration using Nodejs.

After creating your project please make sure you have those dependencies: "@mysql/xdevapi" "body-parser" "express" "mysql"

We will be using MySQL to connect to the database. I have already created a table in my database called "usertest" with columns "id", "name", "country" and "number" make sure "id" is auto-increment as i am not going to insert and update the id in the code.

Happy Coding :)

connection/connect.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var sql = require("mysql");
2+
3+
// Create a connection to the database
4+
var connection = sql.createConnection({
5+
host: "localhost",
6+
user: "root", //mysql username
7+
password: "samer94", //mysql password
8+
database: "usertest" //your database name
9+
});
10+
11+
// open the MySQL connection
12+
connection.connect(error => {
13+
if (error) throw error;
14+
console.log("Successfully connected to the database.");
15+
});
16+
17+
module.exports = connection;

0 commit comments

Comments
 (0)