2. Node.js小试牛刀

来源:互联网 发布:淘宝2000的碳纤维车架 编辑:程序博客网 时间:2024/05/08 17:13

前言:

在第一篇中已经详细的讲诉了Nodejs在windows和Linux的安装和使用。这一篇,小试牛刀,看到底怎么用它。


1. windows 上的第一个Nodejs

我们在百度啊Google啊搜索Nodejs时候,第一个例子都是用Nodejs搭建一个web 服务器,我才不这样干,才怪!`(*∩_∩*)′。我也是用这个例子好了。因为比较直观,而且成功后页面上就能看到,很有成就感和继续学习的欲望。


好。开始。现在开始:


在D:\Node.js\文件夹下面创建一个js文件,为:index.js :

var http = require('http');http.createServer(function (req, res) {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('Hello World\n');}).listen(8080);console.log('Server running at http://127.0.0.1:8080/');

我们在本机上创建了一个web 服务器。接下来,我们运行下代码,使创建web服务器生效:

C:\Users\asus>d:D:\>cd Node.jsD:\Node.js>node index.jsServer running at http://127.0.0.1:8080/


windows下,只能这样hold住着,不能后台运行。接下来,打开浏览器,输入本机的127.0.0.1:8080 就能看到hello word了。表示运行成功。


tips:  我们在listen时候只监听了端口号8080,所以就可以在只要能代表本机ip的任何ip都能访问:


127.0.0.1:8008,localhost:8080,127.x.x.x:8080 都可以访问。127.x.x.x表示本机ip。


那么,我们如何改一下,只能让指定的本机一个ip访问呢:

listen(8080,'localhost');listen(8080,'127.0.0.1');
上面2个,两者没有任何区别,绑定任意一个,localhost和127.0.0.1都可以访问。

listen(8080,'127.0.0.2')
上面一个,这样只能访问127.0.0.2:8080上。


说明:一般我们不会指定ip。只绑定端口号,好处多多,以后再说。


2. Linux 上的第一个Nodejs

Linux上安装Nodejs 已经详细说过,下面直接把index,js 复制到Linux 中,我们放到/usr/local/node.js/ 文件夹下:

我们标识下:Hello Word - Linux 表示是Linux下。

var http = require('http');http.createServer(function (req, res) {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('Hello World - Linux\n');}).listen(8080);console.log('Server running at http://127.0.0.1:8080/');

我们运行:

[root@localhost node.js]# node index.js Server running at http://127.0.0.1:8080/


打开Linux的自身的浏览器,输入127.0.0.1:8080,结果我们也能知道:Helo Word -Linux。


我的Linux是在VM下安装,它的IP是192.168.0.104,所以,我们在windows下的浏览器下运行下:192.168.0.104:8080,结果却出人意料,访问不了????


为毛!!!!!!!!!!!!!!


(其实我也是后来才知道的,囧!)


因为Linux的防火墙策略。iptables 这个服务,会根据端口号,来限制一些端口被远程访问。所以,我们访问不了8080端口的原因,就是iptables里面没有对8080加白名单,被限制住了。


知道问题了。所以有2种解决方案:


1. 关闭 iptables:

[root@localhost node.js]# service iptables stopiptables:清除防火墙规则:                                 [确定]iptables:将链设置为政策 ACCEPT:filter                    [确定]iptables:正在卸载模块:                                   [确定][root@localhost node.js]# 

再启动 node index.js ,然后再windows下访问 192.168.0.104:8080,当当当当!有显示了:Helo Word -Linux。


2. 修改iptables

因为防火墙的重要性,我们最好不要关闭它,那么我们就可以修改它,在它里面加入8080的端口。

/etc/sysconfig/iptables 这是配置文件:

[root@localhost node.js]# vi /etc/sysconfig/iptables# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT

具体是啥意思就可以百度这个配置文件中的各个含义了。我们直接修改:

-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

这一句就是将8080端口加入白名单,那么其他人远程可以访问这个端口。


注意: 这一句,千万别加到最末尾,要加到上一个端口号后面,也就是放到倒数第2句前面,因为,最后2行是总体的一个配置,就结束了,你放到它后面,没起作用。

好,我们加到3306端口的下一行:这样子:


# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT

好。再启动 iptables 服务:

[root@localhost node.js]# service iptables startiptables:应用防火墙规则:                                 [确定][root@localhost node.js]# 

好。下面通过启动node index.js。在windows下输入192.168.0.104:8080,ok,成功显示:Helo Word -Linux。


搞定!



0 0