How to Make a small and Buggy Micro Services App
1 minute read
MICROSERVICES:
- React App –>
client
- Express for Posts –>
posts
- Express for Comments –>
comments
CLIENT – a Create React App
- Make a CRA
- Delete everything in
src
- Add
index.js
- Add
App.js
POSTS – an Express Server
- Make folder
npm init
- Build out
index.js
- Make folder
npm init
- Build out
index.js
client/src/index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root"));
client/src/App.js
import React from "react";
export default () => {
return <div> Blog App! </div>;
};
posts/index.js
const express = require("express");
const bodyParser = require("body-parser");
const { randomBytes } = require("crypto");
const app = express();
app.use(bodyParser.json());
const posts = {};
app.get("/posts", (req, res) => {
res.send(posts);
});
app.post("/posts", (req, res) => {
const id = randomBytes(4).toString("hex");
const { title } = req.body;
console.log("getting here", id, title);
posts[id] = {
id,
title,
};
res.status(201).send(posts[id]);
});
app.listen(4000, () => {
console.log("currently listening on port 4000");
});
const express = require("express");
const bodyParser = require("body-parser");
const { randomBytes } = require("crypto");
const app = express();
app.use(bodyParser.json());
const commentsByPostId = {};
app.get("/posts/:id/comments", (req, res) => {
res.send(commentsByPostId[req.params.id]);
});
app.post("/posts/:id/comments", (req, res) => {
const commentId = randomBytes(4).toString("hex");
const content = req.body;
const comments = commentsByPostId[req.params.id] || [];
comments.push({ id: commentId, content });
commentsByPostId[req.params.id] = comments;
res.status(201).send(comments);
});
app.listen(4001, () => {
console.log("currently listening on port 4001");
});