-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (51 loc) · 1.32 KB
/
server.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
const express = require("express");
const db = require("./models");
const dotenv = require("dotenv");
const jwt = require("jsonwebtoken");
const app = express();
app.use(express.json());
dotenv.config();
const users = [
{
username: `${process.env.USER_NAME}`,
password: `${process.env.PASSWORD}`,
},
];
console.log(users);
db.mongoose
.connect(db.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Connected to the database!");
})
.catch((err) => {
console.log("Cannot connect to the database!", err);
process.exit();
});
app.get("/", (req, res) => {
res.json({ message: "OK" });
});
app.post("/login", (req, res) => {
const { username, password } = req.body;
const user = users.find((u) => {
return u.username === username && u.password === password;
});
if (user) {
const token = jwt.sign(
{ username: user.username },
process.env.ACCESS_TOKEN_SECRET
);
res.status(200).json({ token });
} else {
res.status(400).send({ message: "Username or password incorrect" });
}
});
require("./routes/locations")(app);
require("./routes/cities")(app);
require("./routes/provinces")(app);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});