当前位置: 首页 > 软件库 > 大数据 > 数据查询 >

crae-apollo-heroku

授权协议 Readme
开发语言 Java
所属分类 大数据、 数据查询
软件类型 开源软件
地区 不详
投 递 者 孙翰墨
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Node - Express - Mongoose - GraphQL (Apollo) - Create React App boilerplate ready to be deployed to Heroku

This project is the result of putting together some really nice articles (see) to create a dead simple Node - Express - Mongoose - GraphQL (Apollo) - Create React App boilerplate. The app is deployed to Heroku:https://crae-apollo-heroku.herokuapp.com/

This project includes the following libraries/functionality

  • React 16 (Create React App)
  • Apollo 2
  • Express
  • Mongoose
  • Authentication: passwordless via JWT
  • ES6 syntax
  • service workers

Testing:

  • Apollo Client Mock
  • jest
  • enzyme
  • storybook
  • storyshots

Styling:

  • material-ui
  • styled-components
  • storybook
  • basscss

Step by step guide to get started with this boilerplate

1. Setup MongoDB provider

Before doing anything, we need to setup a Mongo provider to hold our database for us. I'll describe two ways of doing this; choose the one you like the most.

Install Mongo locally

The first approach is to install Mongo locally. In order to so, go to https://docs.mongodb.com/manual/administration/install-community/ and follow the instructions based on your operating system. After that, open a new terminal and start the mongo service; in my case, I'm on Ubuntu, so I run sudo service mongod start. This will start the Mongo service in the background on port 27017.

Get a Sandbox Mongo instance on mLab

The second option is to create a FREE database hosted on mLab and then connect your application to the remote instance. To do so, go to mLab and create a sandbox Mongo instance. Then, go to the Users tab in your mLab-sandbox-MongoDB-instance-dashboard and click on the 'add a database user' button; setup username and password. Remember those values, we'll need them shortly!

2. Create your .env files:

Inside both 'server' and 'client' folders there is a .sample.env file. Create a new file called .env based on the provided .sample.env.

3. Register the app on Mailgun:

Mailgun allows you to send emails from your app.

In order to get started, first access your Mailgun account. Then, grab your sandbox domain smtp username and password and copy said values into your /server/.env file. Finally, add your email address to the list of Auhtorized Recipients.

4. Setup Push Notifications Service

  1. create a new file called manifest.json based on the provided manifest.sample.json (see /public folder).
  2. get your Google Cloud Message (GCM) server key and sender id from Firebase as follows:
  • first, got to your Firebase account: https://console.firebase.google.com/;
  • click on 'Add project';
  • click on 'settings' ('gear' icon, top left);
  • move to the 'CLOUD MESSAGING' tab at the top;
  • you should be able to see both server key and sender id;
  1. copy your sender id to your manifest.json and your server key to GCM_PRIVATE_KEY (/server/.env);
  2. open a terminal and install 'web-push' globally: sudo npm i -g web-push;
  3. generate VAPID keys: web-push generate-vapid-keys --json;
  4. copy-paste your VAPID keys into your VAPID_PUBLIC_KEY, VAPID_PRIVATE_KEY (/server/.env) and REACT_APP_VAPID_PUBLIC_KEY (/client/.env);

5. Running the app locally in dev mode

Once we have our Mongo provider, these are the next steps that we need to follow to run the app locally in dev mode:

  1. Clone the project and move to the project's folder
>> git clone https://github.com/fede-rodes/crae-apollo-heroku.git
>> cd crae-apollo-heroku
  1. Setup your MONGO_URL env variable inside /server/.env to connect the app with your recently created Mongo instance. In case you are using mLab, remember to use your credentials. In case your are running mongo locally, you can use the default value for MONGO_URL.

  2. Install project dependencies, and run the app locally.

>> yarn install
>> yarn start

The app should be running on port 3000 --> http://localhost:3000

Please notice, when running the app locally, there will be two servers running simultaneously: one for serving the create-react-app (CRA) and another one for the Express app. The CRA should be accessible via http://localhost:3000/, and the Express app via http://localhost:3001/. The GraphQL playground should be running on http://localhost:3001/graphql (only accessible in dev mode).

6. Running the app locally in production mode

  1. Follow the steps above to setup your Mongo service.

  2. Install heroku cli: https://devcenter.heroku.com/articles/heroku-cli

  3. Clone the project and move to the project's folder

>> git clone https://github.com/fede-rodes/crae-apollo-heroku.git
>> cd crae-apollo-heroku
  1. Setup your MONGO_URL env variable as describe above.

  2. Install dependencies and run the app locally in production mode.

>> yarn install && yarn build
>> heroku local

This should launch the app on port 5000 --> http://localhost:5000. As far as I understand, the port (process.env.PORT) is setup by heroku and can't be changed.

Please notice, during the build process the CRA is converted into a static asset and injected into the Express app, in this way, there will be only one server running (the one hosting the Express app).

7. Deploy to heroku

  1. Follow the steps above to setup a Mongo service on mLab.

  2. Install heroku cli: https://devcenter.heroku.com/articles/heroku-cli

  3. Clone the project and move to the project's folder

>> git clone https://github.com/fede-rodes/crae-apollo-heroku.git
>> cd crae-apollo-heroku
  1. Initiate Heroku cli and create a new app
>> heroku login (enter your credentials)
>> heroku create <YOUR_APP_NAME>
  1. Install buildpacks (probably not necessary if you don't use yarn) and set MONGO_URL env variable.
heroku buildpacks:set https://github.com/heroku/heroku-buildpack-nodejs#yarn
heroku config:set MONGO_URL=mongodb://<dbuser>:<dbpassword>@<something>.mlab.com:<port>/<dbname>
  1. Push the code to Heroku.
>> git push heroku master

Comment: if you want to deploy from a branch different than master run:

>> git push heroku <BRANCH_NAME>:master

Please notice, similar to the previous case, when the app is deployed to heroku, the CRA is converted into a static asset during the build process and injected into the Express app, ie, there will be only one server running; the port is randomly chosen by heroku and, as far as I understand, this behavior cannot be changed.

8. Heroku deploy troubleshooting

In case your build fails with an error /bin/sh: 1: <SOME-DEP>: not found and you are building the project with yarn, try setting the following env variable:

heroku config:set NPM_CONFIG_PRODUCTION=false

Further reading

GraphQL / Apollo

Progressive Web Apps / Service Workers

PWA off-line support

Service Workers Scripts

styled-components

React / Render Props

Deployment / Heroku

Testing on real devices

 相关资料
  • Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。 Swoft 基于 Apollo 提供的 API,在之上进行封装,使之能在 Swoft 中快速使用。 安装 swoft/whoops 作为一个额外的扩展组件,需要手动安装: Composer 安装 com

  • Apollo以ActiveMQ原型为基础,是一个更快、更可靠、更易于维护的消息代理工具。Apache称Apollo为最快、最强健的 STOMP(Streaming Text Orientated Message Protocol,流文本定向消息协议)服务器。 Apollo的特性如下: 支持Stomp 1.0和Stomp 1.1协议 主题和队列 队列浏览器 主题持久订阅 镜像队列 可靠的消息传递 消

  • Apollo Client 是一个全功能的 GraphQL 客户端,用于 React 、Angular 的交互。允许你轻松通过 GraphQL 获取数据并构建 UI 组件。

  • Glimmer Apollo: Ember and Glimmer integration for Apollo Client. Documentation Visit glimmer-apollo.com to read the docs. Compatibility Apollo Client v3.0 or above GlimmerX v0.6 or above Node.js v12 o

  • Apollo Angular Apollo Angular allows you to fetch data from your GraphQL server and use it in building complex and reactive UIs using the Angular framework. Apollo Angular may be used in any context t

  • apollo-link ⚠️ THIS PROJECT HAS BEEN DEPRECATED ⚠️ The Links in this repo have been migrated to the apollo-client project (as of >= @apollo/client@3.0.0). Please refer to the Apollo Client migration g