[英]How to serve a client side bundle (webpack) to a NodeJS server?
I am having trouble understanding how to bundle my application and then telling my NodeJs server that I want to use that bundle on my root route. 我在理解如何捆绑我的应用程序时遇到麻烦,然后告诉我的NodeJs服务器我想在根路由上使用该捆绑包。 My question is more to understand how I should go about it than specific debugging help, but I am adding some of my folder structure and webpack config to help illustrate my point.
我的问题是比特定的调试帮助更多地了解我应该如何去做,但是我要添加一些文件夹结构和webpack配置以帮助说明我的观点。
I have a fairly simple tic-tac-toe application with a structure like so: 我有一个非常简单的井字游戏应用程序,其结构如下:
tic-tac-toe
src/
app.js (server)
views/
index.html
public/
script/
index.js
...every other JS file
css/
index.css
Let's ignore the server for now and assume I just want to use webpack to bundle all of my JS files. 现在让我们忽略服务器,并假设我只想使用webpack捆绑我的所有JS文件。 With a simple webpack config file, I have:
通过一个简单的webpack配置文件,我有:
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
entry: "./src/public/script/index.js",
output: {
path: path.resolve(__dirname, "./dist/"),
filename: "bundle.client.js"
},
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/
}
]
},
plugins: [new HtmlWebpackPlugin({ template: "./src/views/index.html" })]
}
With the following configuration, when I open my html file in the browser, I have a functional application that loaded the style and all the approriate JS dependencies. 通过以下配置,当我在浏览器中打开html文件时,我有一个正常运行的应用程序,该应用程序加载了样式和所有适当的JS依赖项。
Now the problem comes when I want to serve this html file(which, if I read the doc correctly, was injected with all the required bundles) on a NodeJS server. 现在出现问题了,当我想在NodeJS服务器上提供此html文件(如果我正确阅读了文档,并注入了所有必需的软件包)。 I am unsure as to how I should proceed.
我不确定应该如何进行。 Should I simply use
sendFile
in my node Server with index.html from the dist folder? 我应该只将dist文件夹中的index.html与节点服务器中的
sendFile
一起使用吗? Should I change the entry point of my bundle to be my App.js
Node server? 我是否应该将捆绑软件的入口点更改为
App.js
节点服务器? Do I need a bundle for the client and a different one for the server? 我需要为客户端提供捆绑软件,为服务器提供其他捆绑软件吗? Or am I missing something obvious that prevents me from doing this?
还是我遗漏了一些明显的东西来阻止我这样做?
You're doing everything right: 您做对了所有事情:
You only need a client-side bundle. 您只需要一个客户端捆绑。 On the server, node handles dependencies, so you don't need a server-side bundle.
在服务器上,节点处理依赖项,因此您不需要服务器端捆绑包。
The HTML file generated at dist/index.html
is indeed injected with a <script>
tag pointing to your javascript bundle, thanks to HtmlWebpackPlugin
. 由于
HtmlWebpackPlugin
,在dist/index.html
生成的HTML文件中确实注入了指向您的javascript捆绑包的<script>
标记。
Your intuition ("use sendFile
in your node server") is correct! 您的直觉(“在节点服务器中使用
sendFile
”)是正确的! Note that many frameworks include utility methods to bind a route to a directory containing static files. 请注意,许多框架都包含实用程序方法,可将路由绑定到包含静态文件的目录。 For example, here's how express does it: https://expressjs.com/en/starter/static-files.html
例如,这是表达方式: https : //expressjs.com/en/starter/static-files.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.