44

Boilerplate for MERN stack development

 5 years ago
source link: https://www.tuicool.com/articles/hit/z2UrMze
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

MERN Boilerplate

Boilerplate for MERN stack development and production.

Highlights

Installation

  1. Install dependencies
  2. Set env variables
  3. Add SSL files
  4. Create database

Install dependencies

Run npm install at server folder

Run npm install at client folder

Set env variables - Client

Create .env.development and .env.production files inside client/ folder.

Use port 3002 for development and port 80 for production.

Example (include all of these):

HOST=0.0.0.0
PORT=3002
REACT_APP_HOST=localhost
REACT_APP_PORT=3001
SKIP_PREFLIGHT_CHECK=true
CHOKIDAR_USEPOLLING=true

Located at client/.env.development .

Note: if you change the ports change them in the dockerfiles too (root, server).

Set env variables - Server

Create test.config.env , development.config.env and production.config.env files inside server/.env/ folder.

Use port 3001 for test, development and port 80 for production.

Example (include all of these):

IP=0.0.0.0
HOST=localhost
PORT=3001
CLIENT_HOST=localhost
CLIENT_PORT=3002
SSL_KEY=[SSL_KEY_FILE_NAME]
SSL_CRT=[SSL_CRT_FILE_NAME]
DB_HOST=[MONGOLAB_DB_URL]
DB_USER=[MONGOLAB_DB_USERNAME]
DB_PASS=[MONGOLAB_DB_PASSWORD]
EMAIL_ADDRESS=[GMAIL_ADDRESS]
EMAIL_PASS=[GMAIL_PASSWORD]

Located at server/.env/development.config.env .

Note: if you change the ports change them in the dockerfiles too (root, server).

Add SSL files

Put your (for example) crt.txt and key.txt files inside server/ssl/ folder.

Tip: create them online for free at ZeroSSL

Create database

Create MongoDB with a collection called users .

I've used the free service provided by mLab .

Usage

Note: use the following commands at the root folder.

Development

docker-compose -f docker-compose.development.yml up
https://localhost:3001
http://localhost:3002

Production

npm run build
docker-compose -f docker-compose.production.yml up -d
https://localhost:80

Note: run npm rebuild node-sass inside the client container if asked.

Docker commands

Using separated docker-compose files for development and production.

Development

Start: docker-compose -f docker-compose.development.yml up
Stop: docker-compose -f docker-compose.development.yml down

Production

Start: docker-compose -f docker-compose.production.yml up
Stop: docker-compose -f docker-compose.production.yml down

Without docker

You have to set the environment you use in your scripts at server/package.json :

"test": "set NODE_ENV=test&& mocha --exit --reporter spec \"src/*/*.test.js\"",
"dev": "set NODE_ENV=development&& nodemon app.js -L --exec \"npm run test && npm run lint && node\"",
"build": "set NODE_ENV=production&& webpack --config webpack.config.js",

Just overwrite the test, dev, build lines with the above.

Development

  1. Start npm run dev (or dev:client and dev:server )
  2. Go to https://localhost:3001 (server)
  3. Go to http://localhost:3002 (client)

Production

npm run build
npm start
https://localhost:80

Note: you may need to install nodemon: npm install nodemon -g

NPM Scripts

If you prefer not to use docker, you can use the following scripts from the root folder:

npm run dev

To use this command, you should install concurrently.

It's prepared, just run npm install under the root folder.

npm run dev:client

Runs the react client in development mode.

The browser will lint, reload if you make edits.

npm run dev:server

Runs the node server in development mode.

The server will test, lint and reload if you make edits.

npm run build

Builds the complete application for production to the build folder.

npm start

Runs the app in production mode with PM2 (cluster mode).

npm stop

Stops the application instances in PM2.

npm run delete

Removes the application instances from PM2.

License

The MIT License (MIT)

Copyright (c) 2019 Tamas Szoke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

https://opensource.org/licenses/MIT


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK