0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[MERN11] デプロイ

Last updated at Posted at 2020-12-02
[MERN①] Express & MongoDB Setup
https://qiita.com/niyomong/private/3281af84486876f897f7
[MERN②]User API Routes & JWT Authentication
https://qiita.com/niyomong/private/c11616ff7b64925f9a2b
[MERN③] Profile API Routes
https://qiita.com/niyomong/private/8cff4e6fa0e81b92cb49
[MERN④] Post API
https://qiita.com/niyomong/private/3ce66f15375ad04b8989
[MERN⑤] Getting Started With React & The Frontend
https://qiita.com/niyomong/private/a5759e2fb89c9f222b6b
[MERN⑥] Redux Setup & Alerts
https://qiita.com/niyomong/private/074c27259924c7fd306b
[MERN⑦] React User Authentication
https://qiita.com/niyomong/private/37151784671eff3b92b6
[MERN⑧] Dashboard & Profile Management
https://qiita.com/niyomong/private/ab7e5da1b1983a226aca
[MERN⑨] Profile Display
https://qiita.com/niyomong/private/42426135e959c7844dcb
[MERN⑩] Posts & Comments
https://qiita.com/niyomong/private/19c78aea482b734c3cf5
[MERN11] デプロイ
https://qiita.com/niyomong/private/150f9000ce51548134ad

1. Prepare For Deployment

① config/production.json

XXX/config/production.json
//以下、default.jsonからコピペ
{
  "mongoURI": "xxx",
  "jwtSecret": "xxx"
}

-> gitignoreにconfig/default.json

② 本番環境のビルド最適化

実際には下記は省略 => 代わりにpackage.json+server.jsに記載
Creating an optimized production build...
本番環境にreact&reduxをあげる時に必要。
1. `$ cd client`
2. `$ npm run build`
-> client/buildフォルダができる。
xxxApp/package.json
//...
  "scripts": {
    "start": "node server",
    "server": "nodemon server",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\"",
+    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
  },
//...
xxxApp/server.js
//...
+ const path = require('path');
//...

+ // Serve static assets in production
+ if (process.env.NODE_ENV === 'production') {
+   // Set static folder
+   app.use(express.static('client/build'));

+   app.get('*', (req, res) => {
+     res.sendFile(path.resolve(__dirname, 'client', 'build', + 'index.html'));
+   });
+ }
//...

③ production.jsonはgitignoreされてるのでpushされない

=> gitにコミットせずにHerokuにあげる方法
$ git add -f config/production.json
https://github.com/bradtraversy/devconnector_2.0#deploy-to-heroku

2. Deploy To Heroku

$ heroku create <-最初だけ
$ git add .
$ git git commit -m ""
heroku git:remote -a <AppName> <-最初だけ
git push heroku master

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?