├── client ├── .env ├── public │ ├── robots.txt │ ├── favicon.ico │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── index.html ├── src │ ├── setupTests.js │ ├── App.test.js │ ├── index.css │ ├── index.js │ ├── App.css │ ├── App.js │ ├── logo.svg │ └── serviceWorker.js ├── .gitignore └── package.json ├── .dockerignore ├── scripts ├── start.sh ├── build_local.sh └── redeploy.sh ├── docker-compose.yml ├── .gitignore ├── Dockerfile ├── package.json ├── app.js └── README.md /client/.env: -------------------------------------------------------------------------------- 1 | REACT_APP_HOST=http://localhost:5000 2 | -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- 1 | # npm dependencies 2 | node_modules/ 3 | 4 | # git 5 | .git 6 | .gitignore 7 | -------------------------------------------------------------------------------- /scripts/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | cd client/ 3 | npm run-script build 4 | cd .. 5 | npm start 6 | -------------------------------------------------------------------------------- /client/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /client/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmiyazaki6499/mern-app/HEAD/client/public/favicon.ico -------------------------------------------------------------------------------- /client/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmiyazaki6499/mern-app/HEAD/client/public/logo192.png -------------------------------------------------------------------------------- /client/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rmiyazaki6499/mern-app/HEAD/client/public/logo512.png -------------------------------------------------------------------------------- /scripts/build_local.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | git pull origin master 3 | npm install 4 | cd client 5 | npm install 6 | npm run-script build 7 | cd .. 8 | npm start 9 | -------------------------------------------------------------------------------- /scripts/redeploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | git pull origin master 3 | npm install 4 | cd client 5 | npm install 6 | npm run-script build 7 | cd .. 8 | sudo systemctl restart nginx 9 | sudo pm2 restart all 10 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | mern-app: 5 | build: '.' 6 | env_file: 7 | - 'client/.env' 8 | tty: true 9 | stdin_open: true 10 | ports: 11 | - '5000:5000' 12 | -------------------------------------------------------------------------------- /client/src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /client/src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /client/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # pem file 4 | mern-app.pem 5 | 6 | # dependencies 7 | node_modules/ 8 | client/node_modules/ 9 | */.pnp 10 | *.pnp.js 11 | */package-lock.json 12 | package-lock.json 13 | 14 | # testing 15 | /coverage 16 | 17 | # environment 18 | # .env 19 | 20 | # production 21 | /client/build 22 | 23 | # misc 24 | .DS_Store 25 | .env.local 26 | .env.development.local 27 | .env.test.local 28 | .env.production.local 29 | 30 | npm-debug.log* 31 | yarn-debug.log* 32 | yarn-error.log* 33 | 34 | -------------------------------------------------------------------------------- /client/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /client/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:12.18-alpine3.9 2 | 3 | RUN mkdir /app 4 | WORKDIR /app 5 | 6 | RUN apk update && \ 7 | apk upgrade && \ 8 | apk add git && \ 9 | apk add vim && \ 10 | git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime && \ 11 | sh ~/.vim_runtime/install_awesome_vimrc.sh && \ 12 | sh -c "$(wget -O- https://raw.githubusercontent.com/deluan/zsh-in-docker/master/zsh-in-docker.sh)" 13 | 14 | COPY package.json package.json 15 | COPY client/package.json client/package.json 16 | RUN npm install --silent 17 | RUN cd client/ && npm install --silent 18 | 19 | COPY . . 20 | 21 | LABEL maintainer="Ryuichi Miyazaki " 22 | 23 | CMD ./scripts/start.sh 24 | -------------------------------------------------------------------------------- /client/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | 5 | .App-logo { 6 | height: 40vmin; 7 | pointer-events: none; 8 | } 9 | 10 | @media (prefers-reduced-motion: no-preference) { 11 | .App-logo { 12 | animation: App-logo-spin infinite 20s linear; 13 | } 14 | } 15 | 16 | .App-header { 17 | background-color: #282c34; 18 | min-height: 100vh; 19 | display: flex; 20 | flex-direction: column; 21 | align-items: center; 22 | justify-content: center; 23 | font-size: calc(10px + 2vmin); 24 | color: white; 25 | } 26 | 27 | .App-link { 28 | color: #61dafb; 29 | } 30 | 31 | @keyframes App-logo-spin { 32 | from { 33 | transform: rotate(0deg); 34 | } 35 | to { 36 | transform: rotate(360deg); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^4.2.4", 7 | "@testing-library/react": "^9.3.2", 8 | "@testing-library/user-event": "^7.1.2", 9 | "react": "^16.13.1", 10 | "react-dom": "^16.13.1", 11 | "react-scripts": "3.4.1" 12 | }, 13 | "scripts": { 14 | "start": "react-scripts start", 15 | "build": "react-scripts build", 16 | "test": "react-scripts test", 17 | "eject": "react-scripts eject" 18 | }, 19 | "eslintConfig": { 20 | "extends": "react-app" 21 | }, 22 | "browserslist": { 23 | "production": [ 24 | ">0.2%", 25 | "not dead", 26 | "not op_mini all" 27 | ], 28 | "development": [ 29 | "last 1 chrome version", 30 | "last 1 firefox version", 31 | "last 1 safari version" 32 | ] 33 | }, 34 | "proxy": "http://localhost:5000" 35 | } 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mern-app", 3 | "version": "1.0.0", 4 | "description": "template app for deploying to AWS EC2", 5 | "main": "index.js", 6 | "dependencies": { 7 | "cors": "^2.8.5", 8 | "express": "^4.17.1", 9 | "express-mongo-sanitize": "^2.0.0", 10 | "nodemon": "^2.0.4" 11 | }, 12 | "devDependencies": {}, 13 | "scripts": { 14 | "start": "nodemon app.js", 15 | "test": "echo \"Error: no test specified\" && exit 1" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/rmiyazaki6499/mern-app.git" 20 | }, 21 | "keywords": [ 22 | "mern", 23 | "aws", 24 | "ec2", 25 | "mongodb", 26 | "react", 27 | "express" 28 | ], 29 | "author": "Ryuichi Miyazaki", 30 | "license": "ISC", 31 | "bugs": { 32 | "url": "https://github.com/rmiyazaki6499/mern-app/issues" 33 | }, 34 | "homepage": "https://github.com/rmiyazaki6499/mern-app#readme" 35 | } 36 | -------------------------------------------------------------------------------- /client/src/App.js: -------------------------------------------------------------------------------- 1 | import React, { useState, useEffect } from 'react'; 2 | import logo from './logo.svg'; 3 | import './App.css'; 4 | 5 | function App() { 6 | const [response, setResponse] = useState('Oops, something went wrong...') 7 | useEffect(() =>{ 8 | async function checkAPIRoute() { 9 | try { 10 | const host = process.env.REACT_APP_HOST || "http://localhost:5000" 11 | const response = await fetch( 12 | `${host}/api` 13 | ) 14 | if (response.status === 200) { 15 | setResponse("a Success!!!") 16 | } 17 | } catch (err) { 18 | setResponse("Oops, something went wrong...") 19 | } 20 | } 21 | checkAPIRoute() 22 | }, [response]) 23 | 24 | return ( 25 |
26 |
27 |

Mern-app

28 | logo 29 |

30 | Edit src/App.js and save to reload. 31 |

32 | 38 | 39 |

The API call is...

40 |

{response}

41 |
42 |
43 | ); 44 | } 45 | 46 | export default App; 47 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require("express") 2 | const cors = require("cors") 3 | const path = require("path") 4 | const mongoSanitize = require("express-mongo-sanitize") 5 | 6 | // start app 7 | const app = express() 8 | 9 | // Express port 10 | const port = process.env.PORT || 5000 11 | // const db = process.env.DATABASE 12 | 13 | // CORS 14 | app.use(cors()) 15 | 16 | // Serve static files 17 | app.use(express.static(path.join(__dirname, 'client', 'build'))) 18 | 19 | // Sanitize against NoSQL query injections 20 | app.use(mongoSanitize()) 21 | 22 | // Setting up a route for our API 23 | app.get('/api/', (req, res) => { 24 | return res.status(200).json({ 25 | status: "success" 26 | }) 27 | }) 28 | 29 | // Redirect back to index.html if urls do not match 30 | app.get("*", (req, res) => { 31 | res.sendFile(path.join(__dirname, "client/build", "index.html")) 32 | }) 33 | 34 | // Database 35 | // mongoose 36 | // .connect(db, { 37 | // useNewUrlParser: true, 38 | // useCreateIndex: true, 39 | // useFindAndModify: false, 40 | // useUnifiedTopology: true, 41 | // }) 42 | // .then(() => { 43 | // console.log("Database is connected.") 44 | // }) 45 | // .catch((err) => { 46 | // console.log("Database connection was unsuccessful.") 47 | // console.log(err) 48 | // process.exit(1) 49 | // }) 50 | 51 | app.listen(port, () => { 52 | console.log(`Listening on port ${port}`) 53 | }) 54 | 55 | -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /client/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /client/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then(response => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then(registration => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready 134 | .then(registration => { 135 | registration.unregister(); 136 | }) 137 | .catch(error => { 138 | console.error(error.message); 139 | }); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![MERN Logo](https://www.3ritechnologies.com/wp-content/uploads/2019/11/MERN-Stack-Training-in-Pune-e1575022427244.png) 2 | 3 | # MERN App 4 | 5 | This repository contains a simple MERN app as part of the **Deploying a Production ready React-Express App on EC2 with CI/CD** gist tutorial which you can find [here](https://gist.github.com/rmiyazaki6499/b564b40e306707c8ff6ca9c67d38fb6f) 6 | 7 | ## Table of Contents 8 | 9 | - [Project Layout](#project-layout) 10 | - [Setting up the Mern-app project](#setting-up-the-mern-app-project) 11 | - [Setting up the Mern-app project with Docker](#setting-up-the-mern-app-project-with-docker) 12 | - [Install Docker](#install-docker) 13 | - [Build and Run the Container](#build-and-run-the-container) 14 | - [Cleaning up the Container and Image](#cleaning-up-the-container-and-image) 15 | - [Setting up the Mern-app project manually](#setting-up-the-mern-app-project-manually) 16 | - [Authors](#authors) 17 | 18 | --- 19 | 20 | ## Project Layout 21 | 22 | Here is the project layout: 23 | 24 | ``` 25 | mern-app 26 | |__ client/ (React App Frontend) 27 | |__ public/ 28 | |__ src/ 29 | |__ scripts/ 30 | |__ app.js (Express Backend) 31 | |__ package.json 32 | |__ Dockerfile 33 | |__ docker-compose.yml 34 | 35 | ``` 36 | 37 | ## Project Structure 38 | 39 | The app simply displays the default React app components. However, I have added a simple API which the frontend calls to confirm that the API call is successfull. 40 | 41 | I will be using a generic MERN (MongoDB, Express, React, Node.js) stack app which uses a proxy with the Express server. 42 | 43 | What this means is that instead of having two separate servers running (One for the frontend (React) and the other for the backend (Express)) we will `build` our React project into a directory of static files which Express will then serve. 44 | 45 | The benefit of this is this allows you to manage the project in one collective repo/project and it removes the barrier of having to deal with any CORS issues of your backend not recognizing the frontend requests. 46 | 47 | --- 48 | 49 | ## Setting up the `mern-app` project 50 | 51 | Start by cloning the project with the command: 52 | ``` 53 | $ git clone https://github.com/rmiyazaki6499/mern-app.git 54 | ``` 55 | 56 | ## Setting up the `mern-app` project with Docker 57 | 58 | For those that are not interested in setting up the project manually or would simply not have to worry about downloading node.js and its dependencies, I have created a Dockerfile and docker-compose.yml file to help create a container with everything you would need to run the **mern-app**. 59 | 60 | ### Install Docker 61 | 62 | To make this as easy as possible, we will be using *Docker Compose* to creat our container. 63 | 64 | - If you do not have Docker yet, start by downloading it if you are on a Mac or Windows: 65 | https://www.docker.com/products/docker-desktop 66 | 67 | - Or if you are on a Linux Distribution follow the directions here: 68 | https://docs.docker.com/compose/install/ 69 | 70 | - To confirm you have Docker Compose, open up your terminal and run the command below: 71 | 72 | ``` 73 | $ docker-compose --version 74 | docker-compose version 1.26.2, build eefe0d31 75 | ``` 76 | 77 | - Go into the project directory to build and run the container with: 78 | 79 | ``` 80 | $ cd mern-app/ 81 | $ docker-compose up --build 82 | ``` 83 | 84 | **This may take a few moments** 85 | 86 | Navigate to http://localhost:5000 to view the site on the local server. 87 | It should look something like this: 88 | 89 | ![mern-app_react_success](https://user-images.githubusercontent.com/41876764/87258255-09924080-c457-11ea-97bd-40ad8784a00b.png) 90 | 91 | ### Cleaning up the Container and Image 92 | 93 | - To stop the container from running, use `` twice. 94 | - To close down the container use the command: 95 | 96 | ``` 97 | $ docker-compose down 98 | ``` 99 | - Then to clean up the container and image which we are no longer using use the command: 100 | 101 | ``` 102 | $ docker system prune -fa 103 | ``` 104 | 105 | - Confirm that the container and image is no longer there with: 106 | 107 | ``` 108 | $ docker system df -v 109 | ``` 110 | 111 | ## Setting up the `mern-app` project manually 112 | 113 | - If you either did not want to use Docker or was curious to build the mern-app manually follow the directions below. 114 | 115 | - Start by installing the dependencies for both Express and React: 116 | ``` 117 | $ cd mern-app/ 118 | $ npm install 119 | $ cd client/ 120 | $ npm install 121 | ``` 122 | 123 | Let's first check to see what our React frontend looks like. 124 | - To run the React server use the command in client directory: 125 | ``` 126 | $ npm start 127 | ``` 128 | - Now if you go to http://localhost:3000, you should see something like this: 129 | 130 | ![mern-app_react](https://user-images.githubusercontent.com/41876764/87258089-b8358180-c455-11ea-955a-e182e689c993.png) 131 | 132 | The API is not working because well, we are not running our backend yet! 133 | Let's do that now. 134 | 135 | - In another terminal session run the command `npm start` at the root directory of the project as we did with the frontend. 136 | It should look something like this: 137 | 138 | ![mern-app_run_server](https://user-images.githubusercontent.com/41876764/87258208-ad2f2100-c456-11ea-80c9-7ca9a3624462.png) 139 | 140 | You can see that we have the express server running on port `5000`. 141 | 142 | - Now switch back to the http://localhost:3000 and refresh the page. You should see the Message at the bottom be updated! 143 | 144 | ![mern-app_react_success](https://user-images.githubusercontent.com/41876764/87258255-09924080-c457-11ea-97bd-40ad8784a00b.png) 145 | 146 | We have two servers running, one for the React frontend and one for the Express backend. 147 | If your project set up is essentially two separate projects between the frontend and backend this would be as far as we would need to check. 148 | However, I have set this project set up so that rather than running two servers, we run a reverse proxy for React through Express and will serve the frontend through the Express server. 149 | 150 | Because we will not be running the React server for our project, go ahead and stop the React server. 151 | 152 | - In the `client` directory run the command: 153 | ``` 154 | $ npm run-script build 155 | ``` 156 | 157 | React then will create a `build` directory with a production build of your app which is where our Express server will use to serve the frontend. 158 | 159 | - Now if you go to http://localhost:5000 you should see the same React page from earlier! 160 | 161 | 162 | [Back to Table of Contents](#table-of-contents) 163 | 164 | 165 | ## Author 166 | 167 | Created by: 168 | 169 | - [Ryuichi Miyazaki](https://github.com/rmiyazaki6499) 170 | --------------------------------------------------------------------------------