Skip to content

Commit 423afcc

Browse files
authored
commit
1 parent e7e8d4b commit 423afcc

File tree

21 files changed

+769
-0
lines changed

21 files changed

+769
-0
lines changed

Diff for: 08-rest-api/00-intro/app.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const express = require("express");
2+
const app = express();
3+
4+
app.get("/", (req, res) => {
5+
res.send("Hello world"); // daha önceki derslerdeki render işlemini kullanıyorduk. Burada ise send kullanıyoruz. Send ile bundan sonra veri göndereceğiz.
6+
});
7+
8+
app.listen(3000, () => {
9+
console.log("Server is running on port 3000");
10+
});
11+
12+
/*
13+
? REST API Nedir?
14+
+ REST (Representational State Transfer), bir yazılım mimarisi stili olarak ortaya çıkmış bir kavramdır ve genellikle dağıtık sistemlerde kullanılan bir API (Application Programming Interface) türüdür. REST, özellikle web servisleri aracılığıyla veri iletişimi ve paylaşımı için yaygın olarak kullanılır.
15+
16+
+ REST API, bir yazılım sistemi içindeki kaynaklara (örneğin, veritabanı kayıtları) erişmek ve bu kaynaklarla etkileşimde bulunmak için kullanılan bir tür arayüzdür. Temel olarak, istemcilerin (örneğin, web tarayıcılar veya mobil uygulamalar) sunucudaki kaynaklarla iletişim kurmasını sağlar.
17+
18+
- REST API'nin temel özellikleri şunlar olabilir:
19+
20+
+ 1. **Kaynaklar (Resources)**: Her kaynak, benzersiz bir URI (Uniform Resource Identifier) ile temsil edilir. Örneğin, bir blog uygulaması için "makaleler" veya "kullanıcılar" gibi kaynaklar olabilir.
21+
22+
+ 2. **HTTP Metodları**: REST API, HTTP protokolünü temel alır ve HTTP metotlarını (GET, POST, PUT, DELETE vb.) kaynaklarla ilişkilendirir. Bu metotlar, kaynaklar üzerindeki işlemleri belirtir. Örneğin, GET metoduyla bir kaynağın bilgilerini alabilirsiniz, POST ile yeni bir kaynak oluşturabilirsiniz.
23+
24+
+ 3. **Temel İlkeler (Principles)**: REST, bazı temel prensiplere dayanır. Bunlar arasında sunucu tarafında durumsuzluk (statelessness), önbellek (caching), katmanlılık (layered architecture) gibi kavramlar bulunur.
25+
26+
+ 4. **Veri Formatları**: REST API, genellikle veri iletişimi için JSON veya XML gibi standart veri formatlarını kullanır. Bu formatlar, verileri yapılandırmak ve istemcilerle sunucular arasında paylaşmak için kullanılır.
27+
28+
+ 5. **İstemci-Sunucu İlişkisi**: REST, istemci ve sunucunun ayrı ayrı geliştirilebileceği ve evraklar (documents), veritabanları ve iş mantığı gibi sorumlulukların net bir şekilde ayrıldığı bir yapı sunar.
29+
30+
* REST API, internet üzerinde veri paylaşımı ve iletişimini kolaylaştırmak için yaygın olarak kullanılan bir standart haline gelmiştir. İstemciler, belirli URI'ler kullanarak sunucu tarafındaki kaynaklara erişebilir ve bu kaynaklar üzerindeki işlemleri gerçekleştirebilir.
31+
*/

Diff for: 08-rest-api/00-intro/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "00-intro",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.18.2"
14+
}
15+
}

Diff for: 08-rest-api/01-get-request/app.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const express = require("express");
2+
const app = express();
3+
4+
const products = [
5+
{ id: 1, name: "Product 1", price: 1000 },
6+
{ id: 2, name: "Product 2", price: 2000 },
7+
{ id: 3, name: "Product 3", price: 3000 },
8+
];
9+
10+
app.get("/", (req, res) => {
11+
res.send("Hello world");
12+
});
13+
14+
app.get("/api/products", (req, res) => {
15+
res.send(products);
16+
});
17+
18+
app.get("/api/products/:id", (req, res) => {
19+
const id = req.params.id;
20+
const product = products.find((product) => product.id === parseInt(id));
21+
22+
if (!product) {
23+
return res.status(404).send("Product not found");
24+
}
25+
26+
res.send(product);
27+
});
28+
29+
app.listen(3000, () => {
30+
console.log("Server is running on port 3000");
31+
});

Diff for: 08-rest-api/01-get-request/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "01-get-request",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.18.2"
14+
}
15+
}

Diff for: 08-rest-api/02-post-request/app.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const express = require("express");
2+
const app = express();
3+
4+
// gelen isteklerin(dataların) body kısmını json formatında okumak için
5+
app.use(express.json());
6+
7+
const products = [
8+
{ id: 1, name: "Product 1", price: 1000 },
9+
{ id: 2, name: "Product 2", price: 2000 },
10+
{ id: 3, name: "Product 3", price: 3000 },
11+
];
12+
13+
app.get("/", (req, res) => {
14+
res.send("Hello world");
15+
});
16+
17+
app.get("/api/products", (req, res) => {
18+
res.send(products);
19+
});
20+
21+
app.get("/api/products/:id", (req, res) => {
22+
const id = req.params.id;
23+
const product = products.find((product) => product.id === parseInt(id));
24+
25+
if (!product) {
26+
return res.status(404).send("Product not found");
27+
}
28+
29+
res.send(product);
30+
});
31+
32+
// henüz bir arayüz olmadığı için postman kullanıyoruz
33+
app.post("/api/products", (req, res) => {
34+
const product = {
35+
id: products.length + 1,
36+
name: req.body.name,
37+
price: req.body.price,
38+
};
39+
40+
console.log(req.body);
41+
products.push(product);
42+
43+
res.send(product);
44+
});
45+
46+
app.listen(3000, () => {
47+
console.log("Server is running on port 3000");
48+
});

Diff for: 08-rest-api/02-post-request/package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "02-post-request",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.18.2"
14+
}
15+
}

Diff for: 08-rest-api/03-validation/app.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const express = require("express");
2+
const app = express();
3+
const Joi = require("joi");
4+
5+
// gelen isteklerin(dataların) body kısmını json formatında okumak için
6+
app.use(express.json());
7+
8+
const products = [
9+
{ id: 1, name: "Product 1", price: 1000 },
10+
{ id: 2, name: "Product 2", price: 2000 },
11+
{ id: 3, name: "Product 3", price: 3000 },
12+
];
13+
14+
app.get("/", (req, res) => {
15+
res.send("Hello world");
16+
});
17+
18+
app.get("/api/products", (req, res) => {
19+
res.send(products);
20+
});
21+
22+
app.get("/api/products/:id", (req, res) => {
23+
const id = req.params.id;
24+
const product = products.find((product) => product.id === parseInt(id));
25+
26+
if (!product) {
27+
return res.status(404).send("Product not found");
28+
}
29+
30+
res.send(product);
31+
});
32+
33+
app.post("/api/products", (req, res) => {
34+
// gelen datanın validasyonu için Joi kullanıyoruz
35+
const schema = Joi.object({
36+
name: Joi.string().min(3).required(), // name en az 3 karakterli olmalı
37+
price: Joi.number().min(0).required(), // price 0'dan büyük olmalı
38+
});
39+
40+
const validationResult = schema.validate(req.body);
41+
42+
if (validationResult.error) {
43+
return res.status(400).send(validationResult.error.details[0].message);
44+
}
45+
46+
const product = {
47+
id: products.length + 1,
48+
name: req.body.name,
49+
price: req.body.price,
50+
};
51+
52+
products.push(product);
53+
54+
res.send(product);
55+
});
56+
57+
app.listen(3000, () => {
58+
console.log("Server is running on port 3000");
59+
});

Diff for: 08-rest-api/03-validation/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "03-validation",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.18.2",
14+
"joi": "^17.9.2"
15+
}
16+
}

Diff for: 08-rest-api/04-put-request/app.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const express = require("express");
2+
const app = express();
3+
const Joi = require("joi");
4+
5+
// gelen isteklerin(dataların) body kısmını json formatında okumak için
6+
app.use(express.json());
7+
8+
const products = [
9+
{ id: 1, name: "Product 1", price: 1000 },
10+
{ id: 2, name: "Product 2", price: 2000 },
11+
{ id: 3, name: "Product 3", price: 3000 },
12+
];
13+
14+
app.get("/", (req, res) => {
15+
res.send("Hello world");
16+
});
17+
18+
app.get("/api/products", (req, res) => {
19+
res.send(products);
20+
});
21+
22+
app.get("/api/products/:id", (req, res) => {
23+
const id = req.params.id;
24+
const product = products.find((product) => product.id === parseInt(id));
25+
26+
if (!product) {
27+
return res.status(404).send("Product not found");
28+
}
29+
30+
res.send(product);
31+
});
32+
33+
app.post("/api/products", (req, res) => {
34+
// gelen datanın validasyonu için Joi kullanıyoruz
35+
const validationResult = validateProduct(req.body);
36+
37+
if (validationResult.error) {
38+
return res.status(400).send(validationResult.error.details[0].message);
39+
}
40+
41+
const product = {
42+
id: products.length + 1,
43+
name: req.body.name,
44+
price: req.body.price,
45+
};
46+
47+
products.push(product);
48+
49+
res.send(product);
50+
});
51+
52+
app.put("/api/products/:id", (req, res) => {
53+
const id = req.params.id;
54+
const product = products.find((product) => product.id === parseInt(id));
55+
56+
if (!product) {
57+
return res.status(404).send("Product not found");
58+
}
59+
60+
const validationResult = validateProduct(req.body);
61+
62+
if (validationResult.error) {
63+
return res.status(400).send(validationResult.error.details[0].message);
64+
}
65+
66+
product.name = req.body.name;
67+
product.price = req.body.price;
68+
69+
res.send(product);
70+
});
71+
72+
const validateProduct = (product) => {
73+
const schema = Joi.object({
74+
name: Joi.string().min(3).required(),
75+
price: Joi.number().min(0).required(),
76+
});
77+
78+
return schema.validate(product);
79+
};
80+
81+
app.listen(3000, () => {
82+
console.log("Server is running on port 3000");
83+
});

Diff for: 08-rest-api/04-put-request/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "04-put-request",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"express": "^4.18.2",
14+
"joi": "^17.9.2"
15+
}
16+
}

0 commit comments

Comments
 (0)