Skip to content

Commit c6cd521

Browse files
committed
update dependencies and add routes
Signed-off-by: Erick Wendel <erick.workspace@gmail.com>
1 parent cc48735 commit c6cd521

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ COPY package.json src/package.json
66

77
WORKDIR /src
88

9-
RUN npm install --only=production
9+
RUN npm install --only=production --silent
1010

1111
COPY . /src
1212

index.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const Joi =require('joi')
2-
const Sequelize =require('sequelize')
1+
const Joi = require('joi')
2+
const Sequelize = require('sequelize')
33
const Hapi = require('hapi');
44
const Inert = require("inert");
55
const Vision = require("vision");
@@ -11,6 +11,10 @@ const server = new Hapi.Server(
1111
}
1212
);
1313

14+
const failAction = async (request, h, err) => {
15+
console.error('err', err)
16+
throw err;
17+
}
1418

1519
(async () => {
1620
if (!process.env.POSTGRES_HOST) {
@@ -24,7 +28,7 @@ const server = new Hapi.Server(
2428
ssl: process.env.POSTGRES_SSL,
2529
dialectOptions: {
2630
ssl: process.env.POSTGRES_SSL,
27-
},
31+
},
2832
}
2933
);
3034
await sequelize.authenticate();
@@ -46,9 +50,9 @@ const server = new Hapi.Server(
4650
info: {
4751
title: "Node.js with Postgres Example - Erick Wendel",
4852
version: "1.0",
49-
},
53+
},
5054
}
51-
},
55+
},
5256
]);
5357

5458
server.route([
@@ -64,6 +68,18 @@ const server = new Hapi.Server(
6468
tags: ["api"],
6569
},
6670
},
71+
{
72+
method: "GET",
73+
path: "/heroes/{id}",
74+
handler: (req) => {
75+
return Hero.findAll({ where: { id: req.params.id } });
76+
},
77+
config: {
78+
description: "Get a hero",
79+
notes: "heroes from database",
80+
tags: ["api"],
81+
},
82+
},
6783
{
6884
method: "POST",
6985
path: "/heroes",
@@ -76,13 +92,38 @@ const server = new Hapi.Server(
7692
notes: "create a hero",
7793
tags: ["api"],
7894
validate: {
95+
failAction,
96+
7997
payload: {
8098
name: Joi.string().required(),
8199
power: Joi.string().required(),
82100
},
83101
},
84102
},
85103
},
104+
{
105+
method: "PUT",
106+
path: "/heroes/{id}",
107+
config: {
108+
handler: (req) => {
109+
const { payload } = req;
110+
return Hero.update(payload, { where: { id: req.params.id } });
111+
},
112+
description: "Create a hero",
113+
notes: "create a hero",
114+
tags: ["api"],
115+
validate: {
116+
failAction,
117+
params: {
118+
id: Joi.string().required(),
119+
},
120+
payload: {
121+
name: Joi.string(),
122+
power: Joi.string(),
123+
},
124+
},
125+
},
126+
},
86127

87128
{
88129
method: "DELETE",
@@ -95,6 +136,7 @@ const server = new Hapi.Server(
95136
notes: "Delete a hero",
96137
tags: ["api"],
97138
validate: {
139+
failAction,
98140
params: {
99141
id: Joi.string().required(),
100142
},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"start": "npx pm2-docker index.js"
7+
"start": "npx pm2-docker index.js",
8+
"testAll": "sh run.sh"
89
},
910
"keywords": [],
1011
"author": "erickwendel",

run.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
HOST=localhost:3000
2+
3+
echo '\n\n creating Chapolin'
4+
CREATE=$(
5+
curl --silent -X POST \
6+
--header "Content-Type: application/json" \
7+
--data-binary '{"name":"Chapolin","power":"Strength"}' \
8+
$HOST/heroes
9+
)
10+
11+
echo $CREATE | jq
12+
13+
ID=$(echo $CREATE | jq .id)
14+
15+
echo "\n\n requesting chapolin $ID"
16+
curl --silent $HOST/heroes/$ID | jq
17+
18+
echo '\n\n requesting all heroes'
19+
curl --silent $HOST/heroes | jq
20+
21+
echo "\n\n updating chapolin $ID"
22+
curl --silent -X PUT \
23+
--header "Content-Type: application/json" \
24+
--data-binary '{"name":"Batman","power":"Rich"}' \
25+
$HOST/heroes/$ID \
26+
| jq
27+
28+
echo "\n\n requesting id: $ID"
29+
curl --silent $HOST/heroes/$ID | jq
30+
31+
echo "\n\n removing id: $ID"
32+
curl --silent -X DELETE \
33+
--header "Content-Type: application/json" \
34+
$HOST/heroes/$ID \
35+
| jq
36+
37+
echo '\n\n requesting all heroes'
38+
curl --silent $HOST/heroes | jq

0 commit comments

Comments
 (0)