javascript - 使用http模块将javascript代码包含到node.js中的html中

标签 javascript html node.js http

我正在编写一个聊天室代码,它使用 npm 模块 http 创建一个 http 服务器并提供网页和 mosquitto 来传输数据。但我无法让我的 javascript 代码与 html 一起使用。这是我的 html 代码

<!DOCTYPE html>
<html>
<head>
	<style type="text/css">
		body{
			background-color: black;
		}
		.inp{
			background-color: black;
			color: yellow;
			width: 500px;
			height: 90px
		}
		.hld{
			width: 400px;
			height: 60px;
		}
	</style>
	<title>
		Gobal Chats
	</title>
</head>
<body>
	<div class="hld">
		<input id="tpc" type="text" style="height: 50px; background-color: yellow; color: black; font-size: 25px; border: 2px solid gold" placeholder="enter topic here"><br><br>
		<button onclick="conn()">Connect</button>
		<input  type="text" style="height: 50px; background-color: orange; color: black; font-size: 25px; border: 2px solid gold" placeholder="enter username here" onchange="setName(this.value)"><br><br>
	</div><br><br><br><br><br><br>
	<div class="hld">		
	<input type="text" id="msg" class="inp" placeholder="Type your message here" maxlength="300"></input>
	<button onclick="sendMsg()">Send</button>	
	</div><br><br>
	<div id="messages"></div>
</body>
</html>

这是我的 Javascript 代码

var fs = require('fs');
var http = require('http');
var content = "";
	fs.readFile("./index.html", function read(err,data){
		if(err) throw err;
		content = data;
		console.log(data.toString())
	});		
http.createServer(function(req,res){
	res.writeHead(200, {
		'Content-Type': 'text/html'
	});
	res.write(content);
	res.end();
}).listen(8080,'127.0.0.1');
var mqtt = require('mqtt');
var client = mqtt.connect("mqtt://test.mosquitto.org");
var topic = "";
var name = "";		
	client.on('connect',function(){
		client.subscribe(topic);
		console.log('subscribed')
	})
	function conn(){
		topic = document.getElementById('tpc').value;
	}
	function sendMsg(){
		var mss = document.getElementById('msg').value;
		client.publish(mss);		
	}
	function setName(a){
		name = a;
	}
console.log("server ready")

但是当我运行代码并在浏览器上访问网页时,出现错误,提示我编写的函数无法定义,例如

Uncaught ReferenceError: conn is not defined

其中 conn 是函数之一。这意味着 html 代码没有访问 Javascript 代码。我无法将 Javascript 代码放入 <script> 中html 的标签如 mqtt,http 和 fs 是 Node 模块。那么如何获取 html 代码来访问 Javascript 代码呢?

最佳答案

您正在编写的 JavaScript 代码是 NodeJS 代码,我认为您无法在浏览器中运行该脚本。 您创建的函数(如 conn 和其他使用 npm 模块的函数)将无法在 HTML 中访问,因为代码应该在 Nodejs 环境中的服务器端工作。

如果您热衷于在客户端使用 npm 模块,您可能需要查看 this library .

Here您可以阅读类似的引用资料。

我认为this article将清除您正在尝试混合的内容。

关于javascript - 使用http模块将javascript代码包含到node.js中的html中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473126/

相关文章:

javascript - 无法覆盖 Promise 类上的方法 - Promise.prototype.then 在不兼容的接收器上调用未定义

javascript - NodeJS,多次尝试获取两点之间的 360 度 Angular 失败

javascript - 我如何更改 sequelize 中包含字段的名称?

html - SVG/CSS : Masked Header Logo

html - 为什么现代 Web 浏览器的代码库如此庞大?

html - intel XDK 到底产生什么输出

node.js - 从 Google Cloud Functions Console 访问 MongoDB Atlas 集群

javascript - 停止 Angular 将 [object Object] 插入我的文本输入

javascript - 跨 Sequelize 关联播种数据

javascript - 遍历特定子项 - jQuery/Javascript