简体   繁体   English

如何将 MERN 堆栈部署到托管服务?

[英]How to deploy MERN stack to a hosting service?

I am new to web development and I have a hosting service on which I want to deploy my reactjs/node project.我是 Web 开发的新手,我有一个托管服务,我想在其上部署我的 reactjs/node 项目。 To deploy it previously, I had been simply uploading the build folder created from running npm run build .为了之前部署它,我只是上传了通过运行npm run build创建的构建文件夹。 I have since added a connection to a mongodb database using the MERN stack.从那以后,我使用 MERN 堆栈添加了到 mongodb 数据库的连接。

The project was initially created using create-react-app, then I set up my backend node/mongodb following this tutorial: https://appdividend.com/2018/11/11/react-crud-example-mern-stack-tutorial/ .使用create-反应,应用该项目最初创建,然后设置我的后端节点/ MongoDB的下面这个教程: https://appdividend.com/2018/11/11/react-crud-example-mern-stack-tutorial / . All of my server code is contained within the project folder in an api folder.我的所有服务器代码都包含在 api 文件夹中的项目文件夹中。

I am able to run the project on local host by running npm start , nodemon server.js (from the api folder) and connecting to the mongodb in terminal.通过运行npm startnodemon server.js (来自 api 文件夹)并连接到终端中的 mongodb,我能够在本地主机上运行该项目。 When I try to run it from the hosting service in the same way I did previously, it says it cannot connect to the database, which I assume is due to the fact that the connection is not open on my machine.当我尝试以与之前相同的方式从托管服务运行它时,它说它无法连接到数据库,我认为这是由于连接未在我的机器上打开。

Currently, all the API calls are using axios post to http://localhost:4000/ , which I would also assume will not work on the server.目前,所有 API 调用都使用 axios post 到http://localhost:4000/ ,我也假设它在服务器上不起作用。 I have looked online for information regarding deploying the MERN stack, but all of the ones I have found discuss it in terms of hosting on AWS EC2, which is not what I will be doing.我在网上查找了有关部署 MERN 堆栈的信息,但我发现的所有信息都在 AWS EC2 上进行了托管,这不是我将要做的。

How would I be able to deploy to my hosting service?我如何能够部署到我的托管服务? Are there any references or places I should look for this information?是否有任何参考资料或我应该寻找此信息的地方?

Edit: I have connected to the database using MongoAtlas, so am using a SRV address.编辑:我已经使用 MongoAtlas 连接到数据库,所以我使用的是 SRV 地址。 I think the issue is the axios posts using the localhost address, as the app on the server only works if I have nodemon running on my local machine.我认为问题是使用本地主机地址的 axios 帖子,因为服务器上的应用程序只有在我的本地机器上运行 nodemon 时才有效。 What address should I use instead of this?我应该使用什么地址而不是这个地址?

Currently, all the API calls are using axios post to http://localhost:4000/ , which I would also assume will not work on the server.目前,所有 API 调用都使用 axios post 到http://localhost:4000/ ,我也假设它在服务器上不起作用。

You're correct, that will not work.你是对的,那是行不通的。 That's because if users get your React app , all API calls will be redirect to their localhost .那是因为如果用户获得您的React 应用程序,所有 API 调用都将重定向到他们的localhost Which doesn't hold your API哪个不包含您的 API

When I try to run it from the hosting service in the same way I did previously, it says it cannot connect to the database, which I assume is due to the fact that the connection is not open on my machine.当我尝试以与之前相同的方式从托管服务运行它时,它说它无法连接到数据库,我认为这是由于连接未在我的机器上打开。

That is probably because the MongoDB URL is probably still set to something like 'mongodb://localhost:27017/myapp .这可能是因为 MongoDB URL 可能仍设置为类似'mongodb://localhost:27017/myapp There are 2 main options here.这里有两个主要选项。

  1. Use a "database hosting service" like MongoDB Atlas , you will then need to change your database's URL to point to the one provided by the service使用“数据库托管服务”,MongoDB Atlas ,然后您需要更改数据库的 URL 以指向该服务提供的 URL
  2. Host the database on your server , which will work with the localhost URL将数据库托管在您的服务器上,它将与localhost URL 一起使用

I have looked online for information regarding deploying the MERN stack, but all of the ones I have found discuss it in terms of hosting on AWS EC2, which is not what I will be doing.我在网上查找了有关部署 MERN 堆栈的信息,但我发现的所有信息都在 AWS EC2 上进行了托管,这不是我将要做的。

  • Any server where you have control and flexibility will work in this scenario.您拥有控制权和灵活性的任何服务器都可以在这种情况下工作 You can host all your stuff (server, front-end and database) on it if you want.如果需要,您可以在其上托管所有内容(服务器、前端和数据库)。 EC2 can do all of that but it's not your only option. EC2 可以完成所有这些,但它不是您唯一的选择。 You can also check out similar products from Microsoft and Google, etc...您还可以查看 Microsoft 和 Google 等的类似产品...
  • Another way to go is to deploy each artifact separately which is more scalable but also introduces its own complexity.另一种方法是单独部署每个工件,这更具可扩展性,但也会引入其自身的复杂性。 If you're going this way you may want to look into Docker containers and Kubernetes which are useful to orchestrate distributed systems如果您采用这种方式,您可能需要研究Docker 容器Kubernetes ,它们对编排分布式系统很有用

Edit编辑

Since you're using MongoDB Atlas make sure you white-listed the IP adress of your server由于您使用的是 MongoDB Atlas,请确保将服务器的 IP 地址列入白名单

On the front-end, you'll want to set up an axios configuration that'll point to the correct URL based upon the running environment:在前端,您需要设置一个 axios 配置,该配置将根据运行环境指向正确的 URL:

utils/axiosConfig.js (I like using the name app to keep it inline with express , but you can name it whatever) utils/axiosConfig.js (我喜欢使用名称app使其与express保持一致,但您可以随意命名)

import axios from 'axios';

const env = process.env.NODE_ENV; // current environment

export const app = axios.create({
  baseURL:
    env === 'production'
      ? 'http://example.com/api/' // production
      : 'http://localhost:5000/api/', // development
});

Then you can utilize this configuration wherever you do an API call:然后,您可以在任何进行 API 调用的地方使用此配置:

import { app } from '../utils/axiosConfig.js';

app.get("example");
app.post("example");
...etc

Now, it can send requests to http://localhost:5000/api/example or http://example.com/api/example .现在,它可以向http://localhost:5000/api/examplehttp://example.com/api/example发送请求。

In your client package.json, you can specify the environment:在您的client package.json 中,您可以指定环境:

 "scripts": {
    "start": "NODE_ENV=development webpack-dev-server",
    "build": "NODE_ENV=production webpack",
    "stage": "NODE_ENV=staging webpack",
    "test": "NODE_ENV=testing jest --watchAll --coverage",
  },

Well, yes you will have to deploy your service oa hosting (already established).嗯,是的,您将不得不部署您的服务 oa 托管(已建立)。 There are multiple solutions you can look into there is Digital Ocean Look at these few links有多种解决方案,您可以查看Digital Ocean查看这几个链接

how to setup nodejs app for prod 如何为 prod 设置 nodejs 应用程序

how to install mongodb on ubuntu 如何在ubuntu上安装mongodb

Also there is Heroku.还有Heroku。 for that there's a complete tutorial for that here为此,这里有一个完整的教程

prepare and deploye mern stack .准备和部署 mern 堆栈

As I said, there are multiple solutions other than these as well.正如我所说,除此之外还有多种解决方案。 Hope this helps希望这可以帮助

TLDR; TLDR;

In order to run nodejs based apps you'd need to have your own server.为了运行基于 nodejs 的应用程序,您需要拥有自己的服务器。

Details细节

You probably have shared hosting plans and most of them do not offer several third party and open source projects.您可能有共享托管计划,但其中大多数不提供多个第三方和开源项目。 (nodejs or docker) (nodejs 或 docker)

For startups, I'd recommend to look into对于初创公司,我建议研究一下

  1. How you can get a managed VPS (virtual private server) that offer docker containers.如何获得提供 docker 容器的托管 VPS(虚拟专用服务器)。 Wrap your app in a docker container and upload it to your server.将您的应用程序包装在 docker 容器中并将其上传到您的服务器。 (Amazon, Azure etc). (亚马逊、Azure 等)。

  2. Other cheaper options are to get an unmanaged VPS where you would have to install OS yourself, install dependencies, DB, Firewall etc).其他更便宜的选择是获得非托管 VPS,您必须自己安装操作系统、安装依赖项、数据库、防火墙等)。 Don't get scared by their names.不要被他们的名字吓到。

  3. Buy a dedicated server machine, install all the stuff required, apply for a static IP point your domain to your static IP.购买一台专用服务器机器,安装所需的所有东西,申请一个静态 IP,将您的域指向您的静态 IP。

The third option is scalable and cheaper in the long run but it is a good thing to learn few things about servers and their complications.从长远来看,第三种选择是可扩展且更便宜的,但了解一些有关服务器及其复杂性的知识是一件好事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM