繁体   English   中英

如何在运行客户端服务器代码时在index.html中包含js文件

[英]How to include a js file in index.html while running a client server code

我有以下代码用于运行服务器

server.js:

var http= require('http');
var fs= require('fs');
var file= fs.readFile("./public/index.html", function(error,html) {
  if (error) {
    throw error;
  } else {
    var server= http.createServer(function(req,rspn) {
      rspn.writeHead(200,{"Content-Type":"text/html"});
      rspn.write(html);
      rspn.end();
    });
    server.listen(8000);
  }
});

我的index.html和client.js文件位于公用文件夹中。 我的index.html文件正在关注

<!DOCTYPE html>
<html>
<head>
  <title> WEBCAM </title>
</head>
<body>
  <div>
    <button id='request'>Request Camera</button>
    <script type="text/javascript" src="client.js"></script>
</body>
</html>

我有一些用client.js编写的代码。 现在在运行server.js之后,如果我在浏览器中打开localhost:8000,那么我在控制台中看到在client.js中,相同的index.html代码正在被复制。 它没有读取client.js的内容,我在这里做什么?

检查是否可以解决您的问题:

var http= require('http');
var fs= require('fs');
var server= http.createServer(function(req,rspn) {
      rspn.writeHead(200,{"Content-Type":"text/html"});
      fs.createReadStream("./public/index.html").pipe(rspn)
    }).listen(8000);
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM