Added Nginx, Node and Redis project

Signed-off-by: ajeetraina <ajeetraina@gmail.com>
This commit is contained in:
ajeetraina 2022-03-05 12:23:50 +05:30
parent 468b963db0
commit 657dc7c97f
12 changed files with 183 additions and 0 deletions

View File

@ -0,0 +1,25 @@
version: '3.9'
services:
redis:
image: 'redis:alpine'
ports:
- '6379:6379'
web1:
restart: on-failure
build: ./web1
ports:
- '81:5000'
web2:
restart: on-failure
build: ./web2
ports:
- '82:5000'
nginx:
build: ./nginx
ports:
- '80:80'
depends_on:
- web1
- web2

View File

@ -0,0 +1,3 @@
FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf

View File

@ -0,0 +1,12 @@
upstream loadbalancer {
server web1:5000;
server web2:5000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://loadbalancer;
}
}

View File

@ -0,0 +1,9 @@
FROM node:alpine
WORKDIR /usr/src/app
COPY ./package.json ./
RUN npm install
COPY ./server.js ./
CMD ["npm","start"]

View File

@ -0,0 +1,15 @@
{
"name": "web",
"version": "1.0.0",
"description": "Running Node.js and Express.js on Docker",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.2",
"redis": "3.1.2"
},
"author": "",
"license": "MIT"
}

View File

@ -0,0 +1,23 @@
const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
host: 'redis',
port: 6379
});
app.get('/', function(req, res) {
redisClient.get('numVisits', function(err, numVisits) {
numVisitsToDisplay = parseInt(numVisits) + 1;
if (isNaN(numVisitsToDisplay)) {
numVisitsToDisplay = 1;
}
res.send('Number of visits is: ' + numVisitsToDisplay);
numVisits++;
redisClient.set('numVisits', numVisits);
});
});
app.listen(5000, function() {
console.log('Web application is listening on port 5000');
});

View File

@ -0,0 +1,9 @@
FROM node:alpine
WORKDIR /usr/src/app
COPY ./package*.json ./
RUN npm install
COPY ./server.js ./
CMD ["npm","start"]

View File

@ -0,0 +1,15 @@
{
"name": "web1",
"version": "1.0.0",
"description": "Running Node.js and Express.js on Docker",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.2",
"redis": "3.1.2"
},
"author": "",
"license": "MIT"
}

View File

@ -0,0 +1,24 @@
const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
host: 'redis',
port: 6379
});
app.get('/', function(req, res) {
redisClient.get('numVisits', function(err, numVisits) {
numVisitsToDisplay = parseInt(numVisits) + 1;
if (isNaN(numVisitsToDisplay)) {
numVisitsToDisplay = 1;
}
res.send('web1: Total number of visits is: ' + numVisitsToDisplay);
numVisits++;
redisClient.set('numVisits', numVisits);
});
});
app.listen(5000, function() {
console.log('Web app is listening on port 5000');
});

View File

@ -0,0 +1,9 @@
FROM node:alpine
WORKDIR /usr/src/app
COPY ./package.json ./
RUN npm install
COPY ./server.js ./
CMD ["npm","start"]

View File

@ -0,0 +1,15 @@
{
"name": "web2",
"version": "1.0.0",
"description": "Running Node.js and Express.js on Docker",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.2",
"redis": "3.1.2"
},
"author": "",
"license": "MIT"
}

View File

@ -0,0 +1,24 @@
const express = require('express');
const redis = require('redis');
const app = express();
const redisClient = redis.createClient({
host: 'redis',
port: 6379
});
app.get('/', function(req, res) {
redisClient.get('numVisits', function(err, numVisits) {
numVisitsToDisplay = parseInt(numVisits) + 1;
if (isNaN(numVisitsToDisplay)) {
numVisitsToDisplay = 1;
}
res.send('web2: Total number of visits is: ' + numVisitsToDisplay);
numVisits++;
redisClient.set('numVisits', numVisits);
});
});
app.listen(5000, function() {
console.log('Web app is listening on port 5000');
});