首页 > Web开发 > 详细

Learn Nodejs 01

时间:2015-07-18 02:19:05      阅读:361      评论:0      收藏:0      [点我收藏+]

(1)下载nodejs

选择相应的版本进行下载
?
?
(2)安装nodejs
安装的方式比较多,请baidu下

我这边下载的是“node-v0.12.7-linux-x64.tar.gz”这个版本

(1)上传服务器
(2)解压
tar -zxvf?node-v0.12.7-linux-x64.tar.gz
(3)检测是否安装成功
/database/nodejs/node-v0.12.7-linux-x64/bin
?
?

[root@localhost?bin]#?./node?-v
v0.12.7

?
(3)编写HelloWorld
(3.1)新建工作区域 workspace
(3.2)新建第一个项目helloworld

/database/nodejs/workspace/helloworld

(3.3)编写js
vim first.js
拷贝官方网站上的代码
var http = require(‘http‘);
http.createServer(function (req, res) {
  res.writeHead(200, {‘Content-Type‘: ‘text/plain‘});
  res.end(‘Hello World\n‘);
}).listen(1337, ‘202.102.83.169‘);
console.log(‘Server running at http://202.102.83.169:1337/‘);

[root@localhost?helloworld]#?cat?first.js?

?

var?http?=?require(‘http‘);

?

http.createServer(function?(req,?res)?{

?

??res.writeHead(200,?{‘Content-Type‘:?‘text/plain‘});

?

??res.end(‘Hello?World\n‘);

?

}).listen(1337,?‘202.102.83.169‘);

?

console.log(‘Server?running?at?http://202.102.83.169:1337/‘);

?
注意点:这个时候访问“http://202.102.83.169:1337/”无效
?
?

server.listen(port, [hostname], [backlog], [callback])#

Begin accepting connections on the specified port and hostname.?If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

????里面说到如果忽略了hostname,那么服务器将会接受所有IPV4地址的链接,IPv4地址包括127.0.0.1 localhost和本地IP。没有认真看API,以后要注意。那么这样做就可以实现监听本地IP、localhost、127.0.0.1了:

?
1
2
3
4
5
var?http = require(‘http‘);
http.createServer(function?(req, res) {
??res.writeHead(200, {‘Content-Type‘:?‘text/plain‘});
??res.end(‘Hello World\n‘);
}).listen(1337);
?
?
?
(3.4)启动服务

[root@localhost?bin]#?./node?/database/nodejs/workspace/helloworld/first.js?

?

Server?running?at?http://127.0.0.1:1337/

?
(3.5)启动服务
浏览器访问
?
?
?
?
?
?
?
?
?
?
?
?
?

?
?
?
?
?
?
?
?
?
?
?
?
?

Learn Nodejs 01

原文:http://toknowme.iteye.com/blog/2228181

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!