-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
40 lines (35 loc) · 833 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const express = require("express");
const mysql = require("mysql");
const app = express();
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "123456",
database: "knexdb",
});
//connect to database
connection.connect(err => {
if (err) throw err;
console.log("Db Connected...");
});
app.listen(8000, () => {
console.log("Server is running at port 8000");
});
app.get("/", (req, res) => {
let sql = "SELECT * FROM City";
connection.query(sql, (err, results) => {
if (err) throw err;
res.send({
results: results,
});
});
});
app.get("/create", (req, res) => {
let sql = "INSERT INTO City(cityName) value('Rajkot')";
connection.query(sql, (err, results) => {
if (err) throw err;
res.send({
message: "Insert successfully",
});
});
});