[英]Cannot get a script file node js
我有一个简单的疑问,但无法弄清楚。 我正在从本地主机位置启动节点js应用程序,并通过响应发送html文件。 但是它的节点正在加载html内的脚本文件。
节点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文件
<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
function webRtcInit() {
alert("Inside Script")
}
$(document).on('ready', webRtcInit());
当我正常运行html文件时,将其加载脚本文件。 但是,当我通过节点js发送文件并加载脚本时,出现了错误,它无法获取脚本文件,这是为什么呢?
谢谢
我在您的代码中看到了几个问题:
这将呈现您的html页面,类似地,您需要一个script.js
。
app.get('/', function(req, res, next) {
console.log("before redirection");
res.sendfile('index.html');
});
任一个:
app.get('/script.js', function(req, res, next) {
console.log("before redirection");
res.sendfile('index.html');
});
或通用:
app.use(express.static('static')); // now place your static files in the static folder.
与手头的问题无关,但是在script.js
,它是webRtcInit
而不是webRtcInit()
:
$(document).on('ready', webRtcInit);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.