Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
nodejs和javascript用户代理行为_Javascript_Node.js - Fatal编程技术网

nodejs和javascript用户代理行为

nodejs和javascript用户代理行为,javascript,node.js,Javascript,Node.js,如何让javascript从原始http请求传递useragent信息(IP地址)?当我运行下面的代码时,我总是收到服务器IP(例如1.2.3.4)地址,就像它发出http请求一样 index.html <!DOCTYPE html> <html> <title>ContactForm</title> <body> <h1>Contact Form</h1> <form action="http://1.2

如何让javascript从原始http请求传递useragent信息(IP地址)?当我运行下面的代码时,我总是收到服务器IP(例如1.2.3.4)地址,就像它发出http请求一样

index.html

<!DOCTYPE html>
<html>
<title>ContactForm</title>
<body>
<h1>Contact Form</h1>
<form action="http://1.2.3.4:8080/myaction" method="post" target="_blank">
 Business Name <input type="text" name="businessname"><br>
 User Agent: <input type="text" id="UserAgent" name="useragent">
<input type="submit" value="Submit">
</form>`

以下是我的作品:

app.post('/myaction', function(req, res) {
   console.log(req.ip)
   res.send('Thank you for your inquiry, someone will contact you shorty.');
});
产出:

::ffff:17.###.##.## 
值得一提的是,在我的设置中涉及到两台独立的机器(独立的IP),只有在这种情况下,用户代理IP与服务器IP地址不同

如果浏览器(useragent)和节点服务器是同一台机器,显然您将获得与html
action=”中相同的IPhttp://17.###.##.##:7777/myaction“

修改后的“server.js”文件的以下内容:

var express = require('express'),
path = require('path'),
fs = require('fs'),
bodyParser = require('body-parser')

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, 'server/public')));

app.post('/myaction', function(req, res) {
    console.log(req.ip)
    res.send('Thank you for your inquiry, someone will contact you shorty.');

    fs.appendFile('submission.txt',  'useragent='+JSON.stringify(req.headers)+' ', function(err) {
      if (!err) {
          console.log('Wrote agent headers info to file.txt');
        } else {
          throw err;
        }
      });
});

app.get('/', function(request, response){
    console.log(request.headers)
    response.sendFile('/index.html');
});

app.listen(7777, function() {
  console.log('Server running at http://127.0.0.1:7777/');
});
submission.txt
中的结果数据(此处没有用户代理IP,因为它不在
req.headers
中):


我看不出你在node.js的什么地方获得了用户的ip地址。很抱歉,更新了node.js以包含相关的代码效果很好,不确定这是否是正确的问题,但我在哪里可以找到关于我的
(req,res)
”参数选项的文档?酷!看看这里:,没有比官方文件更好的地方了。
var express = require('express'),
path = require('path'),
fs = require('fs'),
bodyParser = require('body-parser')

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(express.static(path.join(__dirname, 'server/public')));

app.post('/myaction', function(req, res) {
    console.log(req.ip)
    res.send('Thank you for your inquiry, someone will contact you shorty.');

    fs.appendFile('submission.txt',  'useragent='+JSON.stringify(req.headers)+' ', function(err) {
      if (!err) {
          console.log('Wrote agent headers info to file.txt');
        } else {
          throw err;
        }
      });
});

app.get('/', function(request, response){
    console.log(request.headers)
    response.sendFile('/index.html');
});

app.listen(7777, function() {
  console.log('Server running at http://127.0.0.1:7777/');
});
useragent={"host":"17.###.##.##:7777","content-type":"application/x-www-form-urlencoded","origin":"http://localhost:7777","content-length":"24","connection":"keep-alive","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9","referer":"http://localhost:7777/","accept-language":"en-us","accept-encoding":"gzip, deflate"}