如何让 Node.js Express 应用程序中的客户端可以使用脚本文件?
How can I make script files available to the client in a Node.js Express app?
我正在构建一个 Node.js Express 应用程序。当我的客户端 Html 文件试图从服务器获取 .js
文件时(当 运行 <script src="somefolder/script.js"></script>
时),它无法访问它。在 Express 应用程序中使客户端可以访问脚本文件的常用方法是什么?
来自Express API:
Serve static content for the app from the "public" directory in the application directory:
app.use(express.static(__dirname + '/public'));
以上示例假定您将具有以下“./public”文件夹结构:
/public
/javascripts
/custom.js
/stylesheets
/style.css
...
在这种情况下,要获取脚本或您想要的任何内容,您需要编写以下内容url:http://path_to_the_site/javascripts/custom.js
。
注意"javascripts"前不需要写"public"。
我正在构建一个 Node.js Express 应用程序。当我的客户端 Html 文件试图从服务器获取 .js
文件时(当 运行 <script src="somefolder/script.js"></script>
时),它无法访问它。在 Express 应用程序中使客户端可以访问脚本文件的常用方法是什么?
来自Express API:
Serve static content for the app from the "public" directory in the application directory:
app.use(express.static(__dirname + '/public'));
以上示例假定您将具有以下“./public”文件夹结构:
/public /javascripts /custom.js /stylesheets /style.css ...
在这种情况下,要获取脚本或您想要的任何内容,您需要编写以下内容url:http://path_to_the_site/javascripts/custom.js
。
注意"javascripts"前不需要写"public"。