Initial commit
This commit is contained in:
1
blog/posts/.gitignore
vendored
Normal file
1
blog/posts/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
37
blog/posts/index.js
Normal file
37
blog/posts/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
const { randomBytes } = require('crypto');
|
||||
const axios = require('axios');
|
||||
|
||||
const app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(cors());
|
||||
|
||||
const posts = {};
|
||||
|
||||
app.get('/posts', (req, res) => {
|
||||
res.send(posts);
|
||||
});
|
||||
|
||||
app.post('/posts', async (req, res) => {
|
||||
const id = randomBytes(4).toString('hex');
|
||||
const { title } = req.body;
|
||||
posts[id] = { id, title };
|
||||
|
||||
await axios.post('http://localhost:4005/events', {
|
||||
type: 'PostCreated',
|
||||
data: posts[id]
|
||||
}).catch(error => console.log(error));
|
||||
|
||||
res.status(201).send(posts[id]);
|
||||
});
|
||||
|
||||
app.post('/events', (req, res) => {
|
||||
console.log('Received Event', req.body.type);
|
||||
res.send({});
|
||||
});
|
||||
|
||||
app.listen(4000, () => {
|
||||
console.log('Listening on 4000');
|
||||
});
|
||||
1256
blog/posts/package-lock.json
generated
Normal file
1256
blog/posts/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
blog/posts/package.json
Normal file
18
blog/posts/package.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "posts",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "nodemon index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"axios": "^0.21.1",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.17.1",
|
||||
"nodemon": "^2.0.7"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user