Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/45.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何发送变量';从客户端浏览器到node.js服务器的s值?_Node.js - Fatal编程技术网

如何发送变量';从客户端浏览器到node.js服务器的s值?

如何发送变量';从客户端浏览器到node.js服务器的s值?,node.js,Node.js,如何将变量的值从客户端浏览器发送到node.js服务器 localhost:8080/myfile.html var http = require('http'); var url = require('url'); var fs = require('fs'); http.createServer(function (req, res) { fs.readFile(__dirname + req.url, 'utf8', function (err,data) { if (err) { re

如何将变量的值从客户端浏览器发送到node.js服务器

localhost:8080/myfile.html

var http = require('http');
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
fs.readFile(__dirname + req.url, 'utf8', function (err,data) {
if (err) {
res.writeHead(404, {'Content-Type' : 'text/html'});
res.end(JSON.stringify(err));
return;
}

res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);

});

}).listen(8080);
var http=require('http');
让websocketserver=require(“websocket”).server;//包括“websocket”模块
var url=require('url');
var fs=需要('fs');
http.createServer(函数(req,res){
fs.readFile(uuu dirname+req.url,'utf8',函数(err,data){
如果(错误){
res.writeHead(404,{'Content-Type':'text/html'});
res.end(JSON.stringify(err));
返回;
}//旧代码
res.writeHead(200,{'Content-Type':'text/html'});
res.end(数据);
});
}).听(8080);//旧代码
让wsServer=newWebSocketServer({//从http服务器创建websocketserver的实例
httpServer:server,
自动接受连接:false
})
wsServer.on(“请求”,(req)=>{//请求时发生什么(连接)
让连接=请求接受(“/*协议*/,请求来源);
connection.on('message',(msg)=>{
console.log(msg.utf8Data);//将消息数据输出到控制台
});
连接。发送(/*此处某物*/);//发送一些消息
});
client.html或类似的内容
让socket=newwebsocket('ws://ip of server.something:port/');
socket.onopen=function(){console.log(“哦,是的,我连接了~!”);
}//将函数设置为在套接字打开时执行
socket.onmessage=(msg)=>{
console.log(msg.data);//从服务器接收的输出消息数据
}
socket.onclose=()=>{console.log(“服务器关闭”);};
var http = require('http');
let websocketserver = require("websocket").server; // include "websocket" module
var url = require('url');
var fs = require('fs');

http.createServer(function (req, res) {
fs.readFile(__dirname + req.url, 'utf8', function (err,data) {
if (err) {
res.writeHead(404, {'Content-Type' : 'text/html'});
res.end(JSON.stringify(err));
return;
} // old code

res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);

});

}).listen(8080); // old code
let wsServer = new websocketserver({ // create instance of WebSocketServer from http server
 httpServer:server,
 autoAcceptConnections: false
})
wsServer.on("request", (req) => { // what happens on request(connection)
 let connection = req.accept("" /* protocol */, req.origin);
 connection.on('message', (msg) => {
     console.log(msg.utf8Data); // output message data to console
 });
 connection.send(/* something here */); // send some message
});
client.html, or something like this
 <!DOCTYPE html>
 <!-- some basic code here -->
 <script>
 let socket = new WebSocket('ws://ip-of-server.something:port/');
 socket.onopen = function() { console.log("Oh yeah, i connected~!"); 
 } // set function to execute when socket will open
 socket.onmessage = (msg) => {
   console.log(msg.data); // output message data received from server
 }
 socket.onclose = () => { console.log("Server closed"); };
 </script>
 </html>