Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add query service for listing posts and comments
- Loading branch information
Showing
9 changed files
with
1,346 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const cors = require('cors'); | ||
// Dont need axios since it won't need to emit events? | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(cors()); | ||
|
||
// GET /posts | ||
// POST /events | ||
// Decide on a data structure for all the comments | ||
const posts = {}; | ||
|
||
app.get('/posts', (req, res) => { | ||
res.send(posts); | ||
}); | ||
|
||
// receives events from event bus | ||
app.post('/events', (req, res) => { | ||
const { type, data } = req.body; | ||
|
||
if (type === 'PostCreated') { | ||
const { id, title } = data; | ||
|
||
posts[id] = { id, title, comments: [] }; | ||
} | ||
|
||
if (type === 'CommentCreated') { | ||
const { id, content, postId } = data; | ||
const post = posts[postId]; | ||
post.comments.push({ id, content }); | ||
} | ||
// Node doesn't show content of nested structures | ||
// to save on output clutter | ||
// console.log(posts); | ||
// Still a route handle so be sure to return something | ||
res.send({}); | ||
}); | ||
|
||
app.listen(4002, ()=>{ | ||
console.log("Query service listening on 4002") | ||
}) |
Oops, something went wrong.