dongxuan2015 2018-04-20 11:54
浏览 77
已采纳

如何阻止节点js用于第三方的服务器地址和端口

I have phpmyadmin, PHP , nodeJS run on the same server and i use below code to get data from nodejs to my php website. So anyone can copy that code and add to some HTML page and chek what my nodejs server sending on this port. Is there any way to block this nodejs server address use outside from my server??.

This is client side code

var socket  = io.connect('http://xxx.xxx.xx.xx:8000');
socket.on('device', function (data) {
var   commentData = JSON.parse(data);
console.log(commentData);
   var  cID  =  commentData.data.cID;
   var  dID  =  commentData.data.dID;
   var  status  =  commentData.data.status;
if(status =='online'){
    $("#"+cID+dID).attr('class','online-icon');
}
 if(status =='offline'){
        $("#"+cID+dID).attr('class','offline-icon');
    }

});

This is server side code

var express = require('express');
var socket  = require('socket.io');
var http    = require('http').Server(app);
var request = require('request');
var app     = express();
app.use(express.static('views'));
app.set('views', 'views/product'); 
app.use(express.static('./public'));
var bodyParser = require('body-parser')
var server     = app.listen(8000, function () {
});
var io         = socket(server);
io.on('connection', function (socket) {
 io.sockets.emit('device', 'test');
});
  • 写回答

1条回答 默认 最新

  • dongyan5706 2018-04-20 12:01
    关注

    Is there any way to block this nodejs server address use outside from my server?

    Networked computers typically have at least two network interfaces: The "local" one and the network-facing one. If you have services that only need to talk to each other on the server, bind the service to the local one (localhost aka 127.0.0.1).

    You're using express, so you'd do this in your listen call, which is identical to Node.js's own Server.listen. E.g.:

    var server     = app.listen(8000, '127.0.0.1', function () {
    // -------------------------------^^^^^^^^^^^^^
    });
    

    or for IPv6:

    var server     = app.listen(8000, '::1', function () {
    // -------------------------------^^^^^^^
    });
    

    Then no other host on the network will be able to connect to that service.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化