Skip to content

Commit 370d8e4

Browse files
committed
docker compose extension demos started
1 parent 24bb192 commit 370d8e4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+17884
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM node:22-alpine
2+
3+
WORKDIR /app
4+
5+
COPY ["package.json", "package-lock.json*", "./"]
6+
7+
RUN npm install
8+
9+
COPY . .
10+
11+
EXPOSE 8080
12+
13+
CMD npm start
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
GET http://localhost:8080/api/topics HTTP/1.1
2+
3+
###
4+
POST http://localhost:8080/api/topics HTTP/1.1
5+
content-type: application/json
6+
7+
{
8+
"name": "Docker images"
9+
}
10+
11+
###
12+
13+
POST http://localhost:8080/api/topics HTTP/1.1
14+
content-type: application/json
15+
16+
{
17+
"name": "Docker volumes"
18+
}
19+
20+
###
21+
22+
POST http://localhost:8080/api/topics HTTP/1.1
23+
content-type: application/json
24+
25+
{
26+
"name": "Docker networking"
27+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const Topic = require("./models/topics");
2+
const express = require("express"),
3+
mongoose = require("mongoose"),
4+
bodyParser = require("body-parser"),
5+
cors = require("cors"),
6+
app = express();
7+
8+
//configure CORS
9+
app.use(cors());
10+
11+
//configure app to use body-parser
12+
app.use(bodyParser.urlencoded({ extended: true }));
13+
app.use(bodyParser.json());
14+
15+
const port = process.env.PORT || 8080;
16+
17+
const delay = () =>
18+
new Promise((res) => {
19+
setTimeout(() => {
20+
res();
21+
}, 1000);
22+
});
23+
24+
const connect = async () => {
25+
await delay();
26+
try {
27+
await mongoose.connect("mongodb://mongodb:27017/test");
28+
console.log("connected to test");
29+
} catch (error) {
30+
console.error(error);
31+
}
32+
};
33+
34+
//Routes
35+
const router = express.Router();
36+
37+
router.post("/topics", async (req, res) => {
38+
console.log("[POST] Topics");
39+
40+
const topic = new Topic();
41+
topic.name = req.body.name;
42+
43+
try {
44+
const result = await topic.save();
45+
console.log(result);
46+
res.send(result);
47+
} catch (error) {
48+
console.error(error);
49+
res.send(JSON.parse(error));
50+
}
51+
});
52+
53+
router.get("/topics", async (_, res) => {
54+
console.log("[GET] Topics");
55+
console.log(Topic);
56+
57+
try {
58+
const collection = await Topic.find({});
59+
const topics = collection.map((t) => ({
60+
id: t._id.toString(),
61+
name: t.name,
62+
}));
63+
res.send(topics);
64+
} catch (error) {
65+
console.error(err);
66+
res.send(JSON.parse(error));
67+
}
68+
});
69+
70+
//all routes will be prefixed with /api
71+
app.use("/api", router);
72+
73+
//start the server
74+
app.listen(port, async () => {
75+
console.log(`Server up and running on port ${port}`);
76+
await connect();
77+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//models/topics.js
2+
3+
const mongoose = require('mongoose'),
4+
Schema = mongoose.Schema;
5+
6+
const TopicSchema = new Schema({
7+
name: String
8+
});
9+
10+
module.exports = mongoose.model('Topic', TopicSchema);

0 commit comments

Comments
 (0)