Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
364 changes: 320 additions & 44 deletions README.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions nodejs-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use the official Node.js image
FROM node:14

# Create and change to the app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image
COPY package*.json ./

# Install production dependencies
RUN npm install

# Copy local code to the container image
COPY . .

# Run the web service on container startup
CMD [ "node", "index.js" ]
45 changes: 45 additions & 0 deletions nodejs-app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const express = require('express');
const mysql = require('mysql');

const app = express();
const port = 3000;

// Create a MySQL connection
const connection = mysql.createConnection({
host: 'mysql-container',
user: 'myuser',
password: 'mypassword',
database: 'mydatabase'
});

// Connect to MySQL
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL');
});

// Define a route for the root URL
app.get('/', (req, res) => {
res.send('Welcome to the Node.js and MySQL app!');
});

// Define a route to get a random row
app.get('/random', (req, res) => {
const query = 'SELECT * FROM mytable ORDER BY RAND() LIMIT 1';
connection.query(query, (err, results) => {
if (err) {
console.error('Error executing query:', err);
res.status(500).send('Server Error');
return;
}
res.json(results[0]);
});
});

// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
1 change: 1 addition & 0 deletions nodejs-app/node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading