简体   繁体   English

AWS:将 node.js 应用程序传递到 EC2 的步骤

[英]AWS: Steps to pass a node.js application to EC2

I'm newbie with AWS and I'm developing a web application with node.js and react.js.我是 AWS 的新手,正在使用 node.js 和 react.js 开发 Web 应用程序。 My application works fine in my laptop but I want to upload it to AWS EC2.我的应用程序在我的笔记本电脑上运行良好,但我想将它上传到 AWS EC2。

When I simulate a production environment in my laptop, I have a /dist folder where are all the code of the front end and the server code is in /src/server folder.当我在笔记本电脑中模拟生产环境时,我有一个 /dist 文件夹,其中包含前端的所有代码,而服务器代码位于 /src/server 文件夹中。

I have uploaded my app to EC2 and now I'm a little bit lost about the next steps.我已将我的应用程序上传到 EC2,现在我对接下来的步骤有点迷茫。

First, I would like if there is any way to download the modules only if they are not installed Second, I would like to know if its mandatory to use babel in this environment, because in all tutorial that I have followed to make the development these modules are always installed like a dev depencies.首先,我想知道是否有任何方法可以仅在未安装模块的情况下下载模块其次,我想知道在这种环境中是否必须使用 babel,因为在我遵循的所有教程中进行这些开发模块总是像开发依赖一样安装。 So, is it now mandatory to move all babel modules to dependencies?那么,现在是否必须将所有 babel 模块移动到依赖项中? Right now, my script to this two steps is:现在,我这两个步骤的脚本是:

npm -i --production && cross-env NODE_ENV=production babel-node src/server/server.js

If I change babel-node for node then I've got an error with "import" command because I'm not using babel.如果我为 node 更改 babel-node,那么“import”命令会出错,因为我没有使用 babel。

My scripts are:我的脚本是:

在此处输入图片说明

Is there to make a build like I do for the front-end code but for the server code?有没有像我为前端代码所做的那样构建服务器代码? How can I do it?我该怎么做?

Fourt, the server who will be listening the calls to the api will be node server and this will get when I finish to make correctly my aws-start script.第四,将监听 api 调用的服务器将是节点服务器,当我完成正确制作我的 aws-start 脚本时,这将得到。 But ... Which is the best option to server the front-end pages?但是......哪个是服务前端页面的最佳选择?

Sorry, I've got too many questions because this is my first app in AWS.抱歉,我有太多问题,因为这是我在 AWS 中的第一个应用程序。

Edit I:编辑我:

Following the wise advices of @Corrie MacDonald when I build my code I've got this files and folders:在我构建代码时遵循@Corrie MacDonald 的明智建议,我得到了以下文件和文件夹:

在此处输入图片说明

Next, I modify my aws-start script:接下来,我修改我的aws-start脚本:

npm i --production && cross-env NODE_ENV=production node dist/assets/js/bundle.js

But, I've got this error:但是,我有这个错误:

在此处输入图片说明

What am I doing wrong?我究竟做错了什么?

Edit II:编辑二:

My webpack.config.babel.js file is:我的 webpack.config.babel.js 文件是:

import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";
import MiniCssExtractPlugin from "mini-css-extract-plugin";

const devMode = process.env.NODE_ENV !== "production";

console.log("devMode: " + devMode);


module.exports = {
    entry: "./src/client/index.js", //set entry file

    // Resolve to output directory and set file
    output: {
        path: path.resolve("dist/assets"),
        filename: "js/bundle.js",
        publicPath: "/assets"   //It's mandatory to define this publicPath to get access to the website when we reload pages
                                //or we access to them directly with url's which have directories of second level like 
                                //http://localhost:4000/directory-level-1/directory-level-2
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: "./src/client/index.html",    //where is our template
            filename: "../index.html",              //where we are going to put our index.html inside the output directory
            minify: {
                collapseWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true,
                useShortDoctype: true
            }            
        }),
        new MiniCssExtractPlugin({
            filename: "css/bundle.css",
            minify: {
                collapseWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true,
                useShortDoctype: true
            }             
        })
    ],
    //It help us to detect errors. 
    devtool: "source-map", 
    // Set dev-server configuration
    devServer: {
        inline: true,
        contentBase: './dist', 
        port: 3000,
        historyApiFallback: true
    },

    // Add babel-loader to transpile js and jsx files
    module: { 
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use:[
                    { 
                        loader: "babel-loader",
                        query: {
                            presets: [
                                "@babel/preset-react"
                            ]
                        }
                    }
                ]
            },
            {
                use: [
                    devMode ? "style-loader" : MiniCssExtractPlugin.loader, 
                    "css-loader"],
                test: /\.css$/
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "style-loader"
                    },
                    {
                        loader: "css-loader",
                        options: {
                            sourceMap: true
                        }
                    },
                    {
                        loader: "saas-loader", 
                        options: {
                            sourceMap: true
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif|svg)$/,
                loader: "url-loader",
                options: {
                    limit: 10000,
                    publicPath: "/assets/images/",
                    outputPath: "./images/"
                }
            },
            {
                test: /\.(eot|ttf|woff|woff2)$/,
                loader: "url-loader",
                options: {
                    limit: 10000,
                    publicPath: "/assets/fonts/",   //It's mandatory to get access to the fonts when we reload pages or access directly
                    outputPath: "./fonts/"
                }
            }
        ]
    }

}

Edit III:编辑三:

This are the folders of my development environment:这是我的开发环境的文件夹:

在此处输入图片说明

How you can see when I make a build I create the /dist folder with the front-end code, but my server code still being in /src/server folder.当我进行构建时,您如何看到我使用前端代码创建了 /dist 文件夹,但我的服务器代码仍在 /src/server 文件夹中。 How can I create a /dist folder for my server code?如何为我的服务器代码创建 /dist 文件夹? Is that possible?那可能吗?

Without going into a lot of detail about automated building procedures, the steps usually go as follows:无需详细介绍自动化构建程序,步骤通常如下:

  • Build Code -- Here, your source code is built and transpiled into a distributable format, which usually goes into a dist/ folder.构建代码——在这里,你的源代码被构建并转换成可分发的格式,通常进入dist/文件夹。

  • Upload your distributable code.上传您的可分发代码。 -- Here, all of the files you have built should be uploaded (manually or automatically) to your EC2 instance. -- 在这里,您构建的所有文件都应该(手动或自动)上传到您的 EC2 实例。

  • Run a startup script -- Here, any project startup code should be run in order to actually start your server.运行启动脚本——在这里,任何项目启动代码都应该运行以真正启动您的服务器。

You don't need babel in production because your project should already have been built by that point.您不需要在生产中使用 babel,因为到那时您的项目应该已经构建好了。 However, if you are building on the EC2 instance, instead of just uploading your dist, then you will need it.但是,如果您在 EC2 实例上构建,而不仅仅是上传您的 dist,那么您将需要它。

In order to turn your EC2 into a routable, reachable web server, you will need to configure some security and routing policies on AWS.为了将您的 EC2 变成可路由、可访问的 Web 服务器,您需要在 AWS 上配置一些安全和路由策略。 You will need to ensure that the instance has a routable IP (or you can use the automatically generated DNS provided by AWS).您需要确保实例具有可路由的 IP(或者您可以使用 AWS 提供的自动生成的 DNS)。 Secondly, you'll need to ensure that your security policy allows port 80 (at the very least, and any additional ports you need to interact with the server - for HTTPS, SSH or something else.)其次,您需要确保您的安全策略允许端口 80(至少,以及您需要与服务器交互的任何其他端口 - 对于 HTTPS、SSH 或其他东西。)

Once you have all this in place, you should be good.一旦你完成了这一切,你应该会很好。

EDIT编辑

If you want to serve static HTML pages, you will have to ensure that you have set up your EC2 container as a web server with something like Apache.如果要提供静态 HTML 页面,则必须确保已将 EC2 容器设置为使用 Apache 之类的 Web 服务器。 However, I would recommend that you run your Node Server exclusively from the server and host your static webpack bundle on S3 as a static website.但是,我建议您只从服务器运行您的 Node Server,并将您的静态 webpack 包作为静态网站托管在 S3 上。

EDIT 2编辑 2

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

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