May 11, 2024
O Wolfson
Node.js-based web application that takes text input from the user, saves it to a text file, and is deployed using Docker. This tutorial also includes steps to deploy the Dockerized application to Google Cloud.
bashmkdir text-file-app
cd text-file-app
npm init -y
bashnpm install express
app.js file:javascriptconst express = require("express");
const fs = require("fs");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send(
'<form method="POST" action="/save"><input type="text" name="text"/><button type="submit">Save</button></form>'
);
});
app.post("/save", (req, res) => {
const { text } = req.body;
fs.writeFileSync("output.txt", text);
res.send("Text saved to file!");
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
.dockerignore file to exclude node_modules and npm logs:node_modules
npm-debug.log
Dockerfile:Dockerfile# Use the official Node.js 16 image. # https://hub.docker.com/_/node FROM node:16 # Create and change to the app directory. WORKDIR /usr/src/app # Copy application dependency manifests to the container image. # A wildcard is used to ensure both package.json AND package-lock.json are copied. COPY package*.json ./ # Install production dependencies. RUN npm install --only=production # Copy local code to the container image. COPY . . # Run the web service on container startup. CMD [ "node", "app.js" ]
bashdocker build -t text-file-app . docker run -dp 3000:3000 text-file-app
Visit http://localhost:3000 in your browser to see the application.
Follow the instructions to install the Google Cloud SDK.
bashgcloud init
Follow the on-screen instructions to log in and set up your GCP project.
bashgcloud auth configure-docker
Replace [PROJECT-ID] with your GCP project ID.
bashdocker tag text-file-app gcr.io/[PROJECT-ID]/text-file-app docker push gcr.io/[PROJECT-ID]/text-file-app
bashgcloud run deploy --image gcr.io/[PROJECT-ID]/text-file-app --platform managed
Follow the prompts to enable the necessary APIs, choose a region, and allow unauthenticated invocations.
When you deploy your application to Google Cloud Run, it will automatically assign a URL to your application. You can use this URL to access your application from any web browser, anywhere.
After you successfully deploy your application using the gcloud run deploy command, Google Cloud Run will output several pieces of information, including the service URL. This URL is the public address you can use to access your application.
The URL typically looks something like this:
https://your-service-name-a1b2c3d4-uc.a.run.app
Here’s the general process to find or verify your application's URL on Google Cloud Run:
Using Google Cloud Console:
Using the gcloud Command:
gcloud command to list all services and their URLs:
bashgcloud run services list
Once you have your URL, simply enter it into your browser’s address bar. You should see your application's main page, which in the case of your Node.js app, would be a simple form for inputting text to save to a file.
If you encounter any issues accessing your app:
This URL allows you to interact with your application just as you would locally, but now it's hosted in a scalable cloud environment.
This tutorial showed you how to create a simple Node.js application that saves text input to a file, how to Dockerize it, and how to deploy it to Google Cloud. You can now access your application globally via the URL provided by Google Cloud Run.