-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (41 loc) · 1.05 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
const express = require("express");
const app = express();
var bodyParser = require("body-parser");
var cors = require("cors");
// ini
var jsonParser = bodyParser.json();
//itu
var urlencodedParser = bodyParser.urlencoded({ extended: false });
const port = 3000;
app.use(cors());
app.use(jsonParser);
app.use(urlencodedParser);
app.get("/", (req, res) => {
res.json("Hello World!");
});
app.post("/login", (req, res) => {
//Simulasi data dari database
const username = "arga";
const password = "12345678";
// ln 30-44 contoh penerapan untuk mengganti if else
//return semua kondisi failed terlebih dahulu
//check if username is valid
if (req.body.username !== username) {
res.json({
status: "error, username not found",
});
return;
}
//check if password is valid
if (req.body.password !== password) {
res.json({
status: "error, wrong password",
});
return;
}
//return kondisi sukses
res.json({ status: "success" });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});