【发布时间】:2019-08-23 20:49:35
【问题描述】:
我是 AWS 的新手,我正在使用 node.js 和 react.js 开发一个 Web 应用程序。我的应用程序在我的笔记本电脑上运行良好,但我想将其上传到 AWS EC2。
当我在笔记本电脑中模拟生产环境时,我有一个 /dist 文件夹,其中包含前端的所有代码,服务器代码位于 /src/server 文件夹中。
我已将我的应用程序上传到 EC2,现在我对接下来的步骤有点迷茫。
首先,我想知道是否有任何方法可以仅在未安装模块的情况下下载它们 其次,我想知道在这种环境中是否必须使用 babel,因为在我进行开发的所有教程中,这些模块总是像开发依赖项一样安装。那么,现在是否必须将所有 babel 模块移至依赖项?现在,我对这两个步骤的脚本是:
npm -i --production && cross-env NODE_ENV=production babel-node src/server/server.js
如果我将 babel-node 更改为节点,那么“import”命令会出错,因为我没有使用 babel。
我的脚本是:
是否可以像我为前端代码而不是服务器代码进行构建?我该怎么做?
第四,将监听 api 调用的服务器将是节点服务器,当我完成正确地制作我的 aws-start 脚本时,这将得到。但是......哪个是前端页面服务器的最佳选择?
抱歉,我有太多问题,因为这是我在 AWS 中的第一个应用程序。
编辑我:
在构建代码时遵循@Corrie MacDonald 的明智建议,我得到了以下文件和文件夹:
接下来,我修改我的 aws-start 脚本:
npm i --production && cross-env NODE_ENV=production node dist/assets/js/bundle.js
但是,我遇到了这个错误:
我做错了什么?
编辑二:
我的 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/"
}
}
]
}
}
编辑三:
这是我的开发环境的文件夹:
当我进行构建时,您如何看到我使用前端代码创建了 /dist 文件夹,但我的服务器代码仍在 /src/server 文件夹中。如何为我的服务器代码创建 /dist 文件夹?这可能吗?
【问题讨论】:
-
看起来某些依赖项在您的包中可能无法解析。文档指的是什么?有时在捆绑的项目中,您需要添加额外的配置(在这种情况下,在您的 webpack 配置中)以指定不应以默认方式捆绑的第三方和外部依赖项。)
-
我已经用我的 webpack.config.babel.js 文件更新了我的原始帖子。
-
dist/assets/js/bundle.js看起来像客户端代码,它应该由浏览器加载。你不应该尝试在服务器上运行它。 -
好的,那么,我的服务器代码在哪里?如果我没记错的话,当我进行构建时,我只对我的客户端代码进行操作。那么,我该如何构建服务器代码呢?或者这是不可能的?
-
您提到您有一个 Node API。 Node API 应该在您的服务器上运行,并且您的捆绑前端应该托管在网络服务器上。正如我在回答中提到的,我建议您将其作为一个静态 AWS S3 网站,同时在您的 EC2 实例中托管您的节点 API。您也可以从 EC2 提供您的捆绑包,但可能需要进一步配置(除非您获得预设的 AMI)
标签: node.js amazon-web-services npm amazon-ec2 babeljs