Skip to content

Commit 53af66c

Browse files
authored
Merge pull request #8 from dinhngoc87/challenge5
adding challenge5 project
2 parents ae1749c + ca5bfa2 commit 53af66c

File tree

12 files changed

+2824
-0
lines changed

12 files changed

+2824
-0
lines changed

dinhngoc87/challenge5/client.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const axios = require("axios");
2+
function UserServiceClient() {
3+
this.baseURL = process.env.USER_SERVICE_URL || "http://localhost:4000";
4+
}
5+
6+
UserServiceClient.prototype.register = async function (data) {
7+
const config = {
8+
url: `${this.baseURL}/api/users/register`,
9+
method: "POST",
10+
headers: { "Content-Type": "application/json" },
11+
data,
12+
};
13+
const response = await axios(config);
14+
return response.data;
15+
};
16+
17+
module.exports = UserServiceClient;
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const express = require("express");
2+
const app = express();
3+
const port = 4001;
4+
app.use(express.json());
5+
6+
const Joi = require("joi");
7+
const schema = Joi.object({
8+
first_name: Joi.string().alphanum().min(3).max(40).required(),
9+
last_name: Joi.string().alphanum().min(3).max(40).required(),
10+
email: Joi.string()
11+
.email({
12+
minDomainSegments: 2,
13+
tlds: { allow: ["com", "net"] },
14+
})
15+
.required(),
16+
password: Joi.string().pattern(new RegExp("^[a-zA-Z0-9]{8,30}$")),
17+
18+
password_confirmation: Joi.ref("password"),
19+
})
20+
.with("first_name", "last_name")
21+
.with("password", "password_confirmation");
22+
23+
var cmsRouter = express.Router();
24+
cmsRouter.post("/api/user_validation", async (req, res, next) => {
25+
console.log("Input validation");
26+
console.log(req.body);
27+
try {
28+
const { error, value } = schema.validate(req.body);
29+
console.log(error.toString());
30+
res.status(200).json(error.toString());
31+
} catch (err) {
32+
next(err);
33+
}
34+
});
35+
36+
app.use(cmsRouter);
37+
38+
app.use(function (err, req, res, next) {
39+
console.log("Calling actual error handler:");
40+
res.status(200).json(err.toString());
41+
});
42+
43+
app.listen(port, () => {
44+
console.log(`Example app listening on port ${port}`);
45+
});

0 commit comments

Comments
 (0)