-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (68 loc) · 2.33 KB
/
app.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var express = require('express');
var bodyParser = require('body-parser');
var sql = require("mssql");
var app = express();
var port = process.env.port || 3000
app.listen(port, () => {
console.log("3000 portunda servis aktif");
});
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(function(req, res, next) {
//Enabling CORS
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});
var dbConfig = {
user: "sa",
password: "j23xmh8v5",
server: "PLEISTARCHOS",
database: "FlutterDeneme",
};
var executeQuery = function(res, query) {
sql.connect(dbConfig, function(err) {
if (err) {
console.log("Veri tabanına bağlanırken hata oluştu :- " + err);
res.send(err);
} else {
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function(err, rs) {
if (err) {
console.log("Sorgu hatası :- " + err);
res.send(err);
sql.close();
} else {
res.send(rs.recordset);
sql.close();
}
});
}
});
}
//GET METODU
app.get("/api/users", function(req, res) {
var query = "Select * from Login";
executeQuery(res, query);
});
//POST METODU
app.post("/api/user", function(req, res) {
var query = "INSERT INTO Login (kullaniciAdi,sifre) VALUES ('" + req.body.kullaniciAdi + "','" + req.body.sifre + "')";
console.log(query);
executeQuery(res, query);
});
//PUT METODU
app.put("/api/user/:id", function(req, res) {
var query = "UPDATE Login SET kullaniciAdi= '" + req.body.kullaniciAdi + "' , sifre= '" + req.body.sifre + "' WHERE id= " + req.params.id;
console.log(query);
executeQuery(res, query);
});
// DELETE METODU
app.delete("/api/user/:id", function(req, res) {
var query = "DELETE FROM Login WHERE id=" + req.params.id;
console.log(query);
executeQuery(res, query);
});