const http = require(‘http’)
var express = require(‘express’)
var app = express();
//发送请求,获取客户端ip
app.get(’/’, function (req, res) {
var clientIp = getIp(req)
console.log(‘客户端ip’,clientIp)
res.send(‘Hello World’);
})
//通过req的hearers来获取客户端ip
var getIp = function(req) {
var ip = req.headers[‘x-real-ip’] || req.headers[‘x-forwarded-for’] || req.connection.remoteAddres || req.socket.remoteAddress || ‘’;
if(ip.split(’,’).length>0){
ip = ip.split(’,’)[0];
}
return ip;
};
// 指定ipv4格式
var server = app.listen(8081, ‘0.0.0.0’,function () {
var host = server.address().address
var port = server.address().port
console.log(‘服务启动…’)
})
在这个代码模块中 反应 Cannot find module ‘express’