A metadata microservice is a Node.js REST service that records, stores, searches, and manages details associated with each video. It uses Express for HTTP handling and MongoDB for persistence. section 9.6.1 section 10.4.1
The service fits into FlixTube through a small operational and architectural contract:
- Expose GET /videos and return the video metadata collection as JSON. section 9.6.1
- Connect to MongoDB using DBHOST and DBNAME, then read from the videos collection. section 9.6.1 section 10.5.1
- Run independently with npm start or use npm run start:dev for live reload during development. section 10.5.1
- Supply PORT, database settings, and the RabbitMQ connection before startup; MongoDB may run in Docker. section 10.5.1
- Inspect deployed instances with Kubernetes logs, pod descriptions, port forwarding, or an interactive shell. section 11.3.2
A testable service boundary
startMicroservice(dbhost, dbname, port) connects to MongoDB, creates the Express application, registers GET /videos, starts the HTTP server, and returns an object with close behavior. Returning the service object separates startup from process execution and supports later integration testing. section 9.6.1
Listing 9.6 The metadata microservice for unit testing
const express = require("express"); const mongodb = require("mongodb"); async function startMicroservice(dbhost, dbname, port) { const client = await mongodb.MongoClient.connect(dbhost); const db = client.db(dbname); const videosCollection = db.collection("videos"); const app = express(); app.get("/videos", async (req, res) => { #1 const videos = await videosCollection.find() #2 .toArray(); #2 res.json({ #3 videos: videos }); #3 }); #4 const server = app.listen(port) ; #5 return { #6 close: () => { #7 server.close(); #8 client.close(); #9 }, db: db, #10 } ; } async function main() { const PORT = process.env.PORT ; const DBHOST = process.env.DBHOST; const DBNAME = process.env.DBNAME; await startMicroservice(DBHOST, DBNAME, PORT); } if (require.main === module) { main() #11 .then(() => console.log(“Microservice online.”)) .catch(err => { console.error(“Microservice failed to start.”); console.error(err && err.stack || err); }); } else { module.exports = { #12 startMicroservice, }; } #1 Handles requests to the /videos route #2 Finds records for videos in the database #3 Returns the list of videos as JSON #4 Other route handlers go here #5 Starts the Express HTTP server #6 Returns an object that represents our microservice #7 Creates a function that can be used to close our server and database #8 Closes the Express HTTP server #9 Closes the connection to the database #10 Allows the test code to access the database #11 Starts the microservice normally if this script is the main module #12 Otherwise, running under test, so exports the function to allow the test to control how the microservice starts
Use the implementation listing to inspect the MongoDB connection, Express route, JSON response, server startup, and returned service handle. section 9.6.1
