Skip to content

Commit

Permalink
Created api in express
Browse files Browse the repository at this point in the history
  • Loading branch information
Mac authored and Mac committed Feb 1, 2023
1 parent f35f448 commit a4c5d4a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion data/feedback.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"Madhu","email":"[email protected]","title":"Hey whats","message":"I will be back next week"},{"name":"Frank","email":"[email protected]","title":"Best Meetup Ever","message":"I really love this meetup. Please don't let it end."},{"name":"Jane","email":"[email protected]","title":"Meeting Time","message":"Would you consider moving the meeting time 30 minutes to about 6pm. It's tough to make it to the meetings on time right after work."},{"name":"Roy","email":"[email protected]","title":"Great Speaker","message":"I really enjoyed the speaker this month. Would love to hear another presentation."}]
[{"name":"New","email":"[email protected]","title":"aMana aof","message":"ASdas das dasdasd"},{"name":"Make it","email":"[email protected]","title":"asdasdsa","message":"ASDASDas dasdasdasd"},{"name":"Madz","email":"[email protected]","title":"Testerssss","message":"Fdas asslbsksdkasdknasdsadasdsa"},{"name":"Daniel","email":"[email protected]","title":"REST title test","message":"This was sent via REST!"},{"name":"Madhu","email":"[email protected]","title":"Hey whats","message":"I will be back next week"},{"name":"Frank","email":"[email protected]","title":"Best Meetup Ever","message":"I really love this meetup. Please don't let it end."},{"name":"Jane","email":"[email protected]","title":"Meeting Time","message":"Would you consider moving the meeting time 30 minutes to about 6pm. It's tough to make it to the meetings on time right after work."},{"name":"Roy","email":"[email protected]","title":"Great Speaker","message":"I really enjoyed the speaker this month. Would love to hear another presentation."}]
44 changes: 29 additions & 15 deletions routes/feedback.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const { request } = require('express');
const express = require('express');

const { check, validationResult } = require('express-validator');

const validations = [
check('name').trim().isLength({ min: 3 }).escape().withMessage('A name is required'),
check('email').trim().isEmail().normalizeEmail().withMessage('A valid email address is required'),
check('title').trim().isLength({ min: 3 }).escape().withMessage('A title is required'),
check('message').trim().isLength({ min: 5 }).escape().withMessage('A message is required'),
];

const router = express.Router();

module.exports = (params) => {
Expand All @@ -28,19 +34,8 @@ module.exports = (params) => {
}
});

router.post(
'/',
[
check('name').trim().isLength({ min: 3 }).escape().withMessage('A name is required'),
check('email')
.trim()
.isEmail()
.normalizeEmail()
.withMessage('A valid email address is required'),
check('title').trim().isLength({ min: 3 }).escape().withMessage('A title is required'),
check('message').trim().isLength({ min: 5 }).escape().withMessage('A message is required'),
],
async (req, res) => {
router.post('/', validations, async (req, res, next) => {
try {
const errors = validationResult(req);

if (!errors.isEmpty()) {
Expand All @@ -58,8 +53,27 @@ module.exports = (params) => {
};

return res.redirect('/feedback');
} catch (err) {
return next(err);
}
);
});

router.post('/api', validations, async (request, response, next) => {
try {
const errors = validationResult(request);
if (!errors.isEmpty()) {
return response.json({ errors: errors.array() });
}

const { name, email, title, message } = request.body;
await feedbackService.addEntry(name, email, title, message);

const feedback = await feedbackService.getList();
return response.json({ feedback, successMessage: 'Thanks for your feedback' });
} catch (err) {
return next(err);
}
});

return router;
};
5 changes: 5 additions & 0 deletions views/layout/partials/scripts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"
></script>

<% if(locals.artwork) { %>
<script src="/js/pixgrid.js"></script>
<%} %> <% if(locals.feedback) { %>
<script src="/js/feedback.js"></script>
<%} %>
3 changes: 3 additions & 0 deletions views/pages/feedback.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<div class="col-md-4">
<div class="maincontent">
<h1>Send us feedback</h1>

<div class="feedback-status"></div>

<% if(locals.errors) {%>
<div class="alert alert-danger">
<ul>
Expand Down

0 comments on commit a4c5d4a

Please sign in to comment.