如何在Node.js + Express应用程序中正确编写路径?

huangapple go评论48阅读模式
英文:

How to write path in the node.js+express application correctly?

问题

问题
所以,当我构建此应用程序时,我的静态文件在服务器上找不到。 好吧,然后我尝试将 express.static("public") 更改为 express.static(path.resolve("dist", "public"))。 现在它可以工作。
现在我明白,在 index.js 中,所有我的路由都是根据这个特定的文件计算的(未构建的 index.js)。

但问题是,当我托管我的站点时,托管上没有 dist 文件夹。 换句话说,我需要根据构建的脚本(bundle.js 文件)来计算公共文件夹(和其他文件)的路径。

问题
我应该怎么做? 或者我理解错了什么?
感谢你的帮助

英文:

Description<br>
For building my application I'm using webpack(if it is important remark).
I'm trying to create node.js+express application and give the static files via express.static(&quot;public&quot;).

My simplified project structure is following:

project-folder


/server
---index.js
/frontend
---/assets
------other files
---index.html

When application is builded I get one new folder in the root directory with this structure:

project-folder

/dist 
---/public
------/assets
---------other files
------index.html
---bundle.js


/server
---// other above mentioned folders and files
/frontend
---// other above mentioned folders and files

Problem<br>
So, when I builded this application - my static files doesn't find on the server.
Ok, then I tryed to change express.static(&quot;public&quot;) to the express.static(path.resolve(&quot;dist&quot;, &quot;public&quot;)). And now it works.<br>
Now I understand, that in the index.js all my routes are calculating regarding this particular file(index.js - which is not builded).
<br><br>
But, the problem is that - when I will host my site - I will have no the dist folder on the hosting. In other words, I need to compute the path to the public folder(and other files), relative to builded script(bundle.js file).
<br><br>
Question<br>
What should I do? Or what do I misunderstand?<br>
Thank you for your help

答案1

得分: 0

根据我理解,我不需要使用 webpack 来构建我的应用程序。我只需要将本地服务器上传到托管服务,然后运行它。

英文:

As far as I understand, I didn't need to build my application using webpack.
I just needed to upload my local server to the hosting and then to run this one.

huangapple
  • 本文由 发表于 2023年4月4日 15:42:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定