-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (69 loc) · 1.72 KB
/
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
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
77
78
79
80
81
82
83
84
85
const express = require('express');
const fs = require('fs');
const app = express();
const Mahasiswa = require('./models/mahasiswa');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
let mahasiswa = [
{ id: 1, nama: 'andre' },
{ id: 2, nama: 'steven' },
{ id: 3, nama: 'kowek' },
];
const path = './data/text.txt';
// fs.readFile(path, 'utf-8', (err, data) => {
// if (err) {
// console.log('Failed to read file');
// } else {
// console.log(data);
// }
// });
// fs.writeFile(path, 'Aku suka pergi ke gunung', (err) => {
// console.log(err);
// });
// fs.appendFile(path, 'aku suka pergi ke hutan', (err) => {
// console.log(err);
// });
const steven = new Mahasiswa('steven belva', '2208561901', '4.0');
console.log(steven);
const checkUsername = (req, res, next) => {
const { nama } = req.body;
let isExist = false;
mahasiswa.map((mhs) => {
if (mhs.nama === nama) {
isExist = true;
}
});
if (isExist) {
return res.status(400).json({ success: false, msg: 'username sudah dipakai' });
} else {
next();
}
};
app.get('/mahasiswa', (req, res) => {
res.status(200).json({ data: mahasiswa });
});
app.get('/mahasiswa/:id', (req, res) => {
// id yang dicari
const id = Number(req.params.id);
// Looping
mahasiswa.map((mhs) => {
if (mhs.id == id) {
res.status(200).json({ data: mhs });
}
});
});
app.post('/mahasiswa', checkUsername, (req, res) => {
let len = mahasiswa.length;
let lastId = mahasiswa[len - 1].id;
let currId = lastId + 1;
let nama = req.body.nama;
let mahasiswaBaru = {
id: currId,
nama: nama,
};
mahasiswa.push(mahasiswaBaru);
res.status(200).json({ data: mahasiswa });
});
app.listen(5000, () => {
console.log('App is running on localhost 5000');
});