如何在html文件中插入节点js脚本
问题描述:
我已经使用节点Js启动POST服务器。我想通过html文件访问POST服务。如何在html文件中插入节点js脚本
<html>
<body>
<script type="text/javascript">
function PostMethod(){
var http = require('http');
//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
host: 'localhost',
port:8081,
path: '/',
method: 'POST'
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
alert(str);
});
}
http.request(options, callback).end();
}
</script>
<form>
Enter No:<input type="text" id="number" name="number"/><br/>
<input type="button" value="click" onclick="PostMethod()"/>
</form>
</body>
</html>
这是行不通的。但是iF我在节点js中独立运行脚本,我得到的帖子回复
答
您不能直接在浏览器中使用节点模块。你可以试试这个。 https://github.com/substack/http-browserify
看看这个。 http://browserify.org/
+1
其他常见的替代方案将是一个简单的普通旧HTML表单(不需要JavaScript)或jQuery。 – PMV
节点代码并不意味着在客户端运行。它的服务器端(后端) – monssef