[英]Cannot get a script file node js
I have a simple doubt but can't figure it out. 我有一个简单的疑问,但无法弄清楚。 I am starting node js app,from a localhost location and through the response i am sending a html file.
我正在从本地主机位置启动节点js应用程序,并通过响应发送html文件。 But its node loading the script file which is inside the html.
但是它的节点正在加载html内的脚本文件。
Node js file 节点js文件
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
app.get('/', function(req, res, next) {
console.log("before redirection");
res.sendfile('index.html'); });
var server = app.listen(9000);
var options = {
debug: true
}
app.use('/api', ExpressPeerServer(server, options));
server.on('connection', function(id) {
console.log("In Server connection")
});
server.on('disconnect', function(id) {
console.log("server Disconnected")
});
Html File HTML文件
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
<script type = "text/javascript" src="script.js"></script>
</head>
<body>
<h1>ha ha this is Webrtc</h1>
</body>
</html>
script.js script.js
function webRtcInit() {
alert("Inside Script")
}
$(document).on('ready', webRtcInit());
When i normally run the html file its loading the script file. 当我正常运行html文件时,将其加载脚本文件。 But when i send the file through node js and load the script, I am getting the error , that it cannot get the script file, Why is this happening...?
但是,当我通过节点js发送文件并加载脚本时,出现了错误,它无法获取脚本文件,这是为什么呢?
Thanks 谢谢
A node.js server does not serve any files by default (this is different that some other web servers). 默认情况下,node.js服务器不提供任何文件(这与某些其他Web服务器不同)。 So, any file that you want it to serve must have a route for it or some type of middleware that handles it.
因此,您希望其提供服务的任何文件都必须具有用于它的路由或用于处理该文件的某种中间件。
So, your code does have a route for /
, but when the browser parses the index.html file that you return from that route and then tries to load script.js
from your node.js server, you don't have a route for that and the server will return a 404 (not found). 因此,您的代码确实具有
/
的路由,但是当浏览器解析您从该路由返回的index.html文件,然后尝试从node.js服务器加载script.js
,您就没有路由该服务器将返回404(未找到)。
The solution is to create a route for script.js
. 解决方案是为
script.js
创建路由。 Since it's a static resource, you can probably use the express.static capability to serve all your static files. 由于它是静态资源,因此您可以使用express.static功能来服务所有静态文件。 You can read about serving static files in express here .
您可以在此处阅读有关快速提供静态文件的信息 。
I am seeing few problems in your code: 我在您的代码中看到了几个问题:
this renders your html page, similarly, you need one for script.js
. 这将呈现您的html页面,类似地,您需要一个
script.js
。
app.get('/', function(req, res, next) {
console.log("before redirection");
res.sendfile('index.html');
});
either specific: 任一个:
app.get('/script.js', function(req, res, next) {
console.log("before redirection");
res.sendfile('index.html');
});
or generic: 或通用:
app.use(express.static('static')); // now place your static files in the static folder.
unrelated to the problem at hand, but, in script.js
, it is webRtcInit
not webRtcInit()
: 与手头的问题无关,但是在
script.js
,它是webRtcInit
而不是webRtcInit()
:
$(document).on('ready', webRtcInit);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.