-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (53 loc) · 1.26 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
const express = require("express");
const bodyParser = require("body-parser");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_NAME || "todos",
process.env.DB_USER || "todos",
process.env.DB_PASSWORD || "password",
{
host: process.env.DB_HOST || "localhost",
dialect: "postgres",
pool: {
max: 10,
min: 1,
},
}
);
const Todo = sequelize.define("todo", {
title: {
type: Sequelize.STRING,
},
completed: {
type: Sequelize.BOOLEAN,
},
});
sequelize.sync().then(() => {
Todo.count({ title: { [Sequelize.Op.Eq]: "Something" } }).then(
(itemCount) => {
if (itemCount === 0) {
Todo.create({ title: "Something", completed: false });
}
}
);
});
const app = express();
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.send("Hello, World!");
});
app.get("/api/todos", (req, res) => {
Todo.findAll()
.then((data) => res.send(data))
.catch((err) => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving tutorials.",
});
});
});
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});