diff --git a/react-express-mongodb/.dockerignore b/react-express-mongodb/.dockerignore deleted file mode 100644 index 4b92424..0000000 --- a/react-express-mongodb/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -server/node_modules diff --git a/react-express-mongodb/backend/.dockerignore b/react-express-mongodb/backend/.dockerignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/react-express-mongodb/backend/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/react-express-mongodb/backend/Dockerfile b/react-express-mongodb/backend/Dockerfile index c49f8fc..2b39211 100644 --- a/react-express-mongodb/backend/Dockerfile +++ b/react-express-mongodb/backend/Dockerfile @@ -1,27 +1,14 @@ -FROM node:13.13.0-stretch-slim - -#Argument that is passed from docer-compose.yaml file -ARG NODE_PORT - -#Echo the argument to check passed argument loaded here correctly -RUN echo "Argument port is : $NODE_PORT" +FROM node:lts-buster-slim # Create app directory WORKDIR /usr/src/app -#COPY . . -COPY . . +COPY package.json /usr/src/app/package.json +COPY package-lock.json /usr/src/app/package-lock.json +RUN npm ci -# Install app dependencies -# A wildcard is used to ensure both package.json AND package-lock.json are copied -# where available (npm@5+) -RUN npm install - - -#In my case my app binds to port NODE_PORT so you'll use the EXPOSE instruction to have it mapped by the docker daemon: - -EXPOSE ${NODE_PORT} - -CMD npm run dev +COPY . /usr/src/app +EXPOSE 3000 +CMD [ "npm", "run", "dev" ] diff --git a/react-express-mongodb/docker-compose.yml b/react-express-mongodb/docker-compose.yml index e505be6..1056cf1 100644 --- a/react-express-mongodb/docker-compose.yml +++ b/react-express-mongodb/docker-compose.yml @@ -1,12 +1,9 @@ version: "3.7" services: frontend: - build: - context: frontend - args: - FRONT_END_PORT: 5000 + build: frontend ports: - - 5000:5000 + - 3000:3000 stdin_open: true volumes: - ./frontend:/usr/src/app @@ -21,10 +18,7 @@ services: backend: container_name: backend restart: always - build: - context: backend - args: - NODE_PORT: 3000 + build: backend volumes: - ./backend:/usr/src/app - /usr/src/app/node_modules diff --git a/react-express-mongodb/frontend/.dockerignore b/react-express-mongodb/frontend/.dockerignore index 2840226..93f1361 100644 --- a/react-express-mongodb/frontend/.dockerignore +++ b/react-express-mongodb/frontend/.dockerignore @@ -1,3 +1,2 @@ node_modules npm-debug.log -server/logs/ \ No newline at end of file diff --git a/react-express-mongodb/frontend/Dockerfile b/react-express-mongodb/frontend/Dockerfile index 8f21fe3..4bb81b2 100644 --- a/react-express-mongodb/frontend/Dockerfile +++ b/react-express-mongodb/frontend/Dockerfile @@ -1,15 +1,9 @@ # Create image based on the official Node image from dockerhub -FROM node:13.13.0-stretch - -#Argument that is passed from docer-compose.yaml file -ARG FRONT_END_PORT +FROM node:lts-buster-slim # Create app directory WORKDIR /usr/src/app -#Echo the argument to check passed argument loaded here correctly -RUN echo "Argument port is : $FRONT_END_PORT" - # Copy dependency definitions COPY package.json /usr/src/app COPY package-lock.json /usr/src/app @@ -24,7 +18,7 @@ RUN npm ci COPY . /usr/src/app # Expose the port the app runs in -EXPOSE ${FRONT_END_PORT} +EXPOSE 3000 # Serve the app CMD ["npm", "start"] diff --git a/react-express-mongodb/frontend/src/App.js b/react-express-mongodb/frontend/src/App.js index ab5a858..78df4d0 100644 --- a/react-express-mongodb/frontend/src/App.js +++ b/react-express-mongodb/frontend/src/App.js @@ -1,56 +1,55 @@ -import React from 'react'; -import {request} from './utilities/httpRequestHandler' -import './App.scss'; +import React from "react"; +import axios from "axios"; +import "./App.scss"; import AddTodo from "./components/AddTodo"; import TodoList from "./components/TodoList"; -export default class App extends React.Component{ - - constructor(props){ +export default class App extends React.Component { + constructor(props) { super(props); this.state = { - todos: [] - } + todos: [], + }; } componentDidMount() { - request('get','/api') - .then((response) => { - this.setState({ - todos: response.data.data - }) - }).catch((e) => console.log('Error : ', e)) + axios + .get("/api") + .then((response) => { + this.setState({ + todos: response.data.data, + }); + }) + .catch((e) => console.log("Error : ", e)); } - _handleAddTodo = (value) => { - request('post', '/api/todos', {text:value}) - .then((response) => { - let todosCopy = this.state.todos; - todosCopy.unshift({text:value}); - this.setState({ - todos : todosCopy - }); - this.refs.todo.value = "" - }).catch((e) => console.log('Error : ', e)); - }; - + handleAddTodo = (value) => { + axios + .post("/api/todos", { text: value }) + .then(() => { + this.setState({ + todos: [...this.state.todos, { text: value }], + }); + }) + .catch((e) => console.log("Error : ", e)); + }; render() { return ( -
-
-
-
-

Todos

-
- {this._handleAddTodo(value)}}/> - -
+
+
+
+
+

Todos

+
+ +
+
); } } diff --git a/react-express-mongodb/frontend/src/components/AddTodo.js b/react-express-mongodb/frontend/src/components/AddTodo.js index 438bbd1..4a2691a 100644 --- a/react-express-mongodb/frontend/src/components/AddTodo.js +++ b/react-express-mongodb/frontend/src/components/AddTodo.js @@ -1,22 +1,33 @@ -import React from 'react'; +import React from "react"; export default class AddTodo extends React.Component { - - _onAddTodo = () => { - if(this.refs.todo.value.length > 0) { - this.props.handleAddTodo(this.refs.todo.value); - this.refs.todo.value = ''; - } - }; - - render() { - return ( -
- - -
- ) + handleSubmit = (e) => { + e.preventDefault(); + const { value } = e.target.elements.value; + if (value.length > 0) { + this.props.handleAddTodo(value); + e.target.reset(); } -} \ No newline at end of file + }; + + render() { + return ( +
+ + +
+ ); + } +} diff --git a/react-express-mongodb/frontend/src/components/TodoList.js b/react-express-mongodb/frontend/src/components/TodoList.js index 06f3976..8551ed0 100644 --- a/react-express-mongodb/frontend/src/components/TodoList.js +++ b/react-express-mongodb/frontend/src/components/TodoList.js @@ -1,48 +1,49 @@ -import React from 'react'; +import React from "react"; export default class TodoList extends React.Component { + constructor(props) { + super(props); - constructor(props){ - super(props); + this.state = { + activeIndex: 0, + }; + } - this.state = { - activeIndex:0, - } - } + handleActive(index) { + this.setState({ + activeIndex: index, + }); + } - _handleActive(index) { - this.setState({ - activeIndex: index - }) - } + renderTodos(todos) { + return ( +
    + {todos.map((todo, i) => ( +
  • { + this.handleActive(i); + }} + > + {todo.text} +
  • + ))} +
+ ); + } - _renderTodos(todos) { - return ( -
    - { - todos.map((todo, i) => { - return (
  • {this._handleActive(i)}} - > - {todo.text} -
  • ) - }) - } -
- ) - } - - - render() { - let { todos } = this.props; - return ( - todos.length > 0 ? - this._renderTodos(todos) - : -
- No Todos to display -
- ) - } -} \ No newline at end of file + render() { + let { todos } = this.props; + return todos.length > 0 ? ( + this.renderTodos(todos) + ) : ( +
+ No Todos to display +
+ ); + } +} diff --git a/react-express-mongodb/frontend/src/utilities/httpRequestHandler.js b/react-express-mongodb/frontend/src/utilities/httpRequestHandler.js deleted file mode 100644 index 710d38f..0000000 --- a/react-express-mongodb/frontend/src/utilities/httpRequestHandler.js +++ /dev/null @@ -1,16 +0,0 @@ -import axios from 'axios'; - - -export function request (method, uri, data, headers = null, params = null) { - let query = { - method, - url: uri - }; - if (headers !== null) - query.headers = headers; - if (params !== null) - query.params = params; - if (method === 'post' || method === 'put' || method === 'delete' || method === 'patch') - query.data = data; - return axios(query); -}