2016-02-10 54 views
1

我怎樣才能讓JavaScript從原始http請求傳遞useragent信息(IP地址)?當我運行下面的代碼時,我總是收到服務器IP(例如1.2.3.4)地址,就好像它正在發出http請求。nodejs和JavaScript useragent行爲

的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>` 

Node.js的代碼

app.use(bodyParser.urlencoded({ extended: true })); 
app.post('/myaction', function(req, res) { 
    res.send('Thank you for your inquiry, someone will contact you shorty.'); 

app.get('/', function(request, response){ 
    response.sendFile('/home/ubuntu/index.html'); 
}); 

fs.appendFile(timeEntry+'submission.txt', 'useragent='+JSON.stringify(req.headers)+' ', function(err) { 
    if (!err) { 
     console.log('Wrote agent headers info to file.txt'); 
    } else { 
     throw err; 
    } 
    }); 
app.listen(8080, function() { 
    console.log('Server running at http://127.0.0.1:8080/'); 
});` 
+0

我沒有看到你的Node.js你是GET用戶的IP地址。 –

+0

很抱歉,更新了node.js以包含相關代碼 –

+1

工作得很好,不確定這是否是正確的問題,但我可以在哪裏找到有關'(req,res)'參數選項的文檔? –

回答

1

以下工作對我來說:

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地址不同。

如果瀏覽器(用戶代理)和節點服務器是同一臺機器,很明顯,你會得到同樣的IP是在HTML action="http://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):

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"}