Ethereum node auto connect
Publish: 2021-04-27 22:26:58
1. Turn on snap, hold down shift and continue to draw. You will find that the overlapped edges will be pasted automatically (but you can't jump too many nodes at a time). Let's experience it. Hope to help you. Any questions are welcome
2. 获取客户端ip,其他的ip不允许访问
function getClientIp (req) {
var ipAddress;
var forwardIpStr = req.headers['x-forwarded-for'];
if (forwardIpStr) {
var forwardIp = forwardIpStr.split(',');
ipAddress = forwardIp[0];
}
if (!ipAddress) {
ipAddress = req.connection.remoteAdress;
}
if (!ipAddress) {
ipAddress = req.socket.remoteAdress;
}
if (!ipAddress) {
if (req.connection.socket) {
ipAddress = req.connection.socket.remoteAdress;
}
else if (req.headers['remote_addr']) {
ipAddress = req.headers['remote_addr'];
}
else if (req.headers['client_ip']) {
ipAddress = req.headers['client_ip'];
}
else {
ipAddress = req.ip;
}
}
return ipAddress;};
function getClientIp (req) {
var ipAddress;
var forwardIpStr = req.headers['x-forwarded-for'];
if (forwardIpStr) {
var forwardIp = forwardIpStr.split(',');
ipAddress = forwardIp[0];
}
if (!ipAddress) {
ipAddress = req.connection.remoteAdress;
}
if (!ipAddress) {
ipAddress = req.socket.remoteAdress;
}
if (!ipAddress) {
if (req.connection.socket) {
ipAddress = req.connection.socket.remoteAdress;
}
else if (req.headers['remote_addr']) {
ipAddress = req.headers['remote_addr'];
}
else if (req.headers['client_ip']) {
ipAddress = req.headers['client_ip'];
}
else {
ipAddress = req.ip;
}
}
return ipAddress;};
3. Of course, datasource doesn't need to be closed. You can think of it as a connection pool directly. Once closed, you have to create a connection pool again, which costs a lot. Access to the database is actually inter process communication. Since it is inter process communication, it must be TCP protocol in the transport layer, and the cost of establishing a connection must be greater than that of obtaining a connection between memory.
4. Steps of building nodejs server:
1. Install nodejs service (download and install from the official website), node is equivalent to Apache server
2. Create a new server file in the directory defined by myself, such as server. JS
for example, I create a new server file in E: & 92; PhpProject\ html5\ The server.js file is created under websocket
var HTTP = require (# 39; http');// Introce HTTP mole / / start the service, and listen to 8888 port / / the port number should be more than 6000. Var server = http.createserver (function (req, RES) {
/ *
req is used to receive client data
res is used to send server data to client
* /
console.log (& # 39; There is a client connection// The successful connection is displayed in the background
/ / the first parameter is the HTTP request status, and the 200 connection is successful
/ / after the connection is successful, the header information is written to the client
res.writeheader (200, {& # 39; content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />
res.write(' This is the main body// Display to client
res.end()< br />
}).listen(8888);< br />
console.log(' Server started successfully
3. In the CMD console, switch the CD to the directory where the server.js is located, and then execute the node server.js command
when the console displays "server started successfully", it means that the node server has been established
4. Access the server in the browser
Enter
in the browser localhost:8888 The browser displays "this is the body part."
view the CMD console and display "with client connection"
the above operations can be performed in multiple browser windows, and each browser window will have a corresponding "with client connection"
the above steps are completed, and the node service is set up. The following is how to access the text / HTML text file of the local site through the built node service
access the local site file
1. Create the node service file server2. JS in the user-defined directory
var HTTP = require (# 39; http'); var fs = require(' fs');// The file reading mole var DocumentRoot = ' is introced; E:/PhpProject/html5/websocket/www';// The directory where the files need to be accessed is var server = http. Createserver (function (req, RES) {
var url = req. URL
/ / the URL entered by the client, for example, if localhost:8888/index.html
/ / then url = = / index. HTML
var file = DocumentRoot + URL< br />console.log(url); // E: / phpproject / HTML5 / websocket / www / index. HTML
FS. Readfile (file, function (err, data) {
/ *
the first parameter is the file path
the second parameter is the callback function
the first parameter of the callback function is the information returned by reading error, and there is no error if it is empty
the second parameter is the text content returned by reading success
* /
if (ERR) {
res.writeheader (404, { ' content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />res.write('& lt; h1> 404 error & lt/ h1>& lt; p> The page you are looking for does not exist & lt/ p> 39;);< br />res.end();< br />}else{
res.writeHeader(200,{ ' content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />res.write(data);// Display index.html on the client
res.end()< br />
}
});< br />
}).listen(8888);< br />
console.log(' Server started successfully;
2. Create index.html file
if you want to access index.html file, of course you must have this file first, otherwise the server will fail to read it, Return to 40412
3. Switch to the directory of server2.js on the CD of the CMD console and execute the node server2.js command
open the server
4. Enter the localhost:8888/index.html Access the file
1. Install nodejs service (download and install from the official website), node is equivalent to Apache server
2. Create a new server file in the directory defined by myself, such as server. JS
for example, I create a new server file in E: & 92; PhpProject\ html5\ The server.js file is created under websocket
var HTTP = require (# 39; http');// Introce HTTP mole / / start the service, and listen to 8888 port / / the port number should be more than 6000. Var server = http.createserver (function (req, RES) {
/ *
req is used to receive client data
res is used to send server data to client
* /
console.log (& # 39; There is a client connection// The successful connection is displayed in the background
/ / the first parameter is the HTTP request status, and the 200 connection is successful
/ / after the connection is successful, the header information is written to the client
res.writeheader (200, {& # 39; content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />
res.write(' This is the main body// Display to client
res.end()< br />
}).listen(8888);< br />
console.log(' Server started successfully
3. In the CMD console, switch the CD to the directory where the server.js is located, and then execute the node server.js command
when the console displays "server started successfully", it means that the node server has been established
4. Access the server in the browser
Enter
in the browser localhost:8888 The browser displays "this is the body part."
view the CMD console and display "with client connection"
the above operations can be performed in multiple browser windows, and each browser window will have a corresponding "with client connection"
the above steps are completed, and the node service is set up. The following is how to access the text / HTML text file of the local site through the built node service
access the local site file
1. Create the node service file server2. JS in the user-defined directory
var HTTP = require (# 39; http'); var fs = require(' fs');// The file reading mole var DocumentRoot = ' is introced; E:/PhpProject/html5/websocket/www';// The directory where the files need to be accessed is var server = http. Createserver (function (req, RES) {
var url = req. URL
/ / the URL entered by the client, for example, if localhost:8888/index.html
/ / then url = = / index. HTML
var file = DocumentRoot + URL< br />console.log(url); // E: / phpproject / HTML5 / websocket / www / index. HTML
FS. Readfile (file, function (err, data) {
/ *
the first parameter is the file path
the second parameter is the callback function
the first parameter of the callback function is the information returned by reading error, and there is no error if it is empty
the second parameter is the text content returned by reading success
* /
if (ERR) {
res.writeheader (404, { ' content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />res.write('& lt; h1> 404 error & lt/ h1>& lt; p> The page you are looking for does not exist & lt/ p> 39;);< br />res.end();< br />}else{
res.writeHeader(200,{ ' content-type' : 39; text/html; charset=" utf-8" 39;< br />});< br />res.write(data);// Display index.html on the client
res.end()< br />
}
});< br />
}).listen(8888);< br />
console.log(' Server started successfully;
2. Create index.html file
if you want to access index.html file, of course you must have this file first, otherwise the server will fail to read it, Return to 40412
3. Switch to the directory of server2.js on the CD of the CMD console and execute the node server2.js command
open the server
4. Enter the localhost:8888/index.html Access the file
5.
varos=require('os');
varhttp=require('http');
varfs=require('fs');
varserver=http.createServer(function(req,res){
if(/^/a.html/.test(req.url)){
fs.createReadStream('a.html').pipe(res);
}else{
console.log(req.connection.remoteAddress+':'+req.connection.remotePort);
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('HelloWorld
');
}
}).listen(8124);
server.setTimeout(0);//设置不超时,所以服务端不会主动关闭连接
console.log('start'+os.hostname()+':8124');6. PM2 is recommended. If there is an error, the service will be restarted automatically.
7. [oracle@node1 ~]$ ssh node2 date
Mon Aug 5 17:33:26 CST 2013
[oracle@node1 ~]$ date
Mon Aug 5 17:33:31 CST 2013
[oracle@node1 ~]$ ssh node1 date
oracle@node1's password:
如图,在node1机器上用oracle用户登录,访问node2不用密码,
Mon Aug 5 17:33:26 CST 2013
[oracle@node1 ~]$ date
Mon Aug 5 17:33:31 CST 2013
[oracle@node1 ~]$ ssh node1 date
oracle@node1's password:
如图,在node1机器上用oracle用户登录,访问node2不用密码,
Hot content
