-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6301c79
Showing
5 changed files
with
1,511 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FROM node:12 | ||
|
||
WORKDIR /usr/src/app | ||
COPY package*.json ./ | ||
RUN npm i | ||
COPY . . | ||
|
||
EXPOSE 80 | ||
CMD ["node", "server.js"] |
Empty file.
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,16 @@ | ||
{ | ||
"name": "my-backend-api", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node server.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@google-cloud/pubsub": "^3.3.0", | ||
"express": "^4.18.2" | ||
} | ||
} |
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 @@ | ||
// server.js | ||
const express = require("express"); | ||
const app = express(); | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID'; | ||
const data = JSON.stringify({foo: 'bar'}); | ||
|
||
// Imports the Google Cloud client library | ||
const {PubSub} = require('@google-cloud/pubsub'); | ||
|
||
// Creates a client; cache this for further use | ||
const pubSubClient = new PubSub(); | ||
|
||
async function publishMessage() { | ||
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject) | ||
const dataBuffer = Buffer.from(data); | ||
|
||
try { | ||
const messageId = await pubSubClient | ||
.topic(test-topic) | ||
.publishMessage({data: dataBuffer}); | ||
console.log(`Message ${messageId} published.`); | ||
} catch (error) { | ||
console.error(`Received error while publishing: ${error.message}`); | ||
process.exitCode = 1; | ||
} | ||
} | ||
|
||
app.get("/user/:id", (req, res) => { | ||
const id = req.params.id; | ||
res.json({ | ||
id, | ||
name: `John Doe #${id}` | ||
}); | ||
|
||
publishMessage(); | ||
}); | ||
|
||
app.listen(8080, () => { | ||
console.log("Server running on port 8080"); | ||
}); |
Oops, something went wrong.