Node.js Express赢得't提供绝对的URL.js文件

Node.js Express won't serve absolute URL .js files

本文关键字:js URL 文件 赢得 Express Node      更新时间:2023-09-26

我在/public/widget中有两个文件,help.html和help.js

http://localhost:8084/widget/help.html

地址栏工作良好

但是

http://localhost:8084/widget/help.js

没有提供("Cannot GET"),尽管我的快速使用配置:

app.use(express.static(path.join(application_root, 'public')));

html文件中具有相对路径的任何js都可以使用。(即,如果我使用

  <script src="/widget/help.js"></script>

生活是美好的;但是

  <Script src="http://localhost:8084/widget/help.js"></script>

不提供。(我需要在远程页面中引用.js的绝对url)

这是我的完整Express配置:

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(morgan('dev')); // log every request to the console
//checks request.body for HTTP method overrides
app.use(methodOverride('X-HTTP-Method-Override'))    //checks request.body for HTTP method overrides
//Where to serve static content
app.use(express.static(path.join(application_root, 'public')));

我一定在做傻事!有什么想法吗?

express没有任何问题。

"我需要在远程页面中引用.js的绝对url"。

因此,您有一些远程系统试图从本地主机加载文件。是的,那行不通。远程系统上的"localhost"与机器上的"本地主机"不同。您需要向远程系统提供您的IP地址(或映射到该IP的域名),而不是本地主机URL。