gpt4 book ai didi

javascript - 不能这样做 : NodeJS server creates number, 通过 JSON 将其发送到客户端

转载 作者:太空宇宙 更新时间:2023-11-04 00:49:57 25 4
gpt4 key购买 nike

我的问题:

服务器生成一个随机数并通过 JSON 将其发送到客户端。当我在不同的窗口上打开客户端时,它显示相同的数字。如果我单击刷新按钮,两个客户端上的数字都会立即更改。

我对 Javascript、Node.js 等还不熟悉,因为这是我在德国作为软件工程师学生的第一个月,我只习惯了 Java(不是 Javascript)和 HTML。

   //my server:
var http = require('http');
var fs = require('fs');

http.createServer(function (req, res) {
fs.readFile('index.html', 'utf-8', function (err, content) {
if (err) {
res.end('error occurred');
return;
}

res.writeHead(200, {'Content-Type': 'text/html'});
res.write(content);
res.end();
});
}).listen(8124);

var ranNum = '{"number": "' + parseInt(Math.random() * 100000000) + '"}';

console.log("server at http://localhost:8124/");

最佳答案

一些解释(阅读代码中的注释)

//my server:
var http = require('http');
var fs = require('fs');



// You create an http server <------------------------------------ this process finish here
http.createServer(function (req, res) {// instantly
// everything here happen after the end of your process |
// of http.createServer |
// |
// |
// here we have launched our server and are |
// waiting for any connection. |
// |
// |
// each a connection reach the server |
// we open a file called 'index.html' |
fs.readFile('index.html', 'utf-8', function (err, content) {// |
if (err) {// if an error occur on reading |
// we send a response to the client |
// to inform the webbrowser about the error |
res.end('error occurred');// |
return;// |
}// |
// if no error we write a response to the client |
res.writeHead(200, {'Content-Type': 'text/html'});// |
// we tell 200 everything ok |
// in the format of : 'Content-Type': 'text/html' |
res.write(content);// we insert in the response the content |
// the content of the file we have read, just now |
// |
res.end();// we send the response to the client |
});// |
}).listen(8124); // you tell the server to listen on port 8124 <----------
// ^
// |
// |-------------------|
// °
// you have to call this port (8124) from
// your webbrowser to reach the http server you just created.


// here you create a variable called ranNum
// you created this one time only
var ranNum = '{"number": "' + parseInt(Math.random() * 100000000) + '"}';

// ranNum should now be something like
// ranNum = '{"number": "37567307"}';


console.log("server at http://localhost:8124/");

// add this line to test :
console.log("ranNum : " , ranNum);

//your console will log the same thing one time only.

因此实际上要为每个客户发送不同的号码,您必须执行类似的操作:

var http = require('http');

var server = http.createServer(function(request, response) {

console.log("we have a request : " + new Date());

var ranNum = '{"number": "' + parseInt(Math.random() * 100000000) + '"}';
console.log("ranNum sent : " , ranNum);

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

});

server.listen(8124);

console.log("server at http://localhost:8124/");

关于javascript - 不能这样做 : NodeJS server creates number, 通过 JSON 将其发送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33082378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com