Node.js Express不会提供绝对URL .js文件

时间:2014-09-21 17:34:12

标签: javascript node.js express

我在/ public / widget,help.html和help.js

中有两个文件
http://localhost:8084/widget/help.html
地址栏中的

可以正常使用

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

未投放('无法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>

未送达。 (我需要绝对的URL来引用远程页面中的.js)

这是我的完整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')));

我一定是在做一些愚蠢的事!有什么想法吗?

1 个答案:

答案 0 :(得分:1)

快递没有错。

“我需要绝对网址来引用远程网页中的.js”。

所以你有一些远程系统试图从你的localhost加载一个文件。是的,那不行。远程系统上的“localhost”与计算机上的“localhost”不同。您需要为远程系统提供IP地址(或映射到该IP的域名),而不是localhost URL。