Ubuntu学习总结-07 Nodejs和npm的安装

一 安装NodeJS

1 下载nodejs源码

  从以下网址下载最新的Nodejs源码

https://nodejs.org/en/download/

2 安装依赖的 python,gcc,g++ 函数库

  运行以下安装依赖包的命令。

sudo apt-get install python 
sudo apt-get install build-essential
sudo apt-get install gcc
sudo apt-get install g++

  把node-v6.2.0.tar.gz上传至Ubuntu,解压缩安装包。

tar -zxf node-v6.2.0.tar.gz
cd node-v6.2.0

3 编译并安装nodejs

  把nodejs安装在/opt/nodejs目录下。

./configure    
make                                              
make install

  编译完成后,执行node发现还是不正确,执行

whereis node

  发现node安装到了/usr/local/bin/node,于是执行这一步创建链接:

sudo ln -s /usr/local/bin/node /usr/bin/node

4 测试安装nodejs

  在控制台输入查看版本命令和帮助命令。

### 查看版本
node --version node -v

### 查看帮助 node
--help

 二 安装 npm

  npm是node package manager的简称,npm是NodeJS的模块管理,执行以下命令,由于新版的nodejs已经集成了npm,所以npm也一并安装好了。同样可以通过输入 "npm -v" 来测试是否成功安装。命令如下,出现版本提示表示安装成功。

npm -v

三 NodeJS的使用

  Node.js是一个Javascript运行环境(runtime)。实际上它是对Google V8引擎进行了封装。V8引 擎执行Javascript的速度非常快,性能非常好。Node.js对一些特殊用例进行了优化,提供了替代的API,使得V8在非浏览器环境下运行得更好。
Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快、易于扩展的网络应用。Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非常适合在分布式设备上运行数据密集型的实时应用。
node有两大特点:

  •   基于事件驱动。
  •   无阻塞。

  无阻赛的特点使nodeJS非常适合处理并发请求,nodeJS其本质就是JavaScript,基于事件驱动(click, focus,mouseOver)。

  JavaScript是单线程,所以要做到无阻塞,node是通过大量的回调函数来达到这一目的。

1 进入NodeJS控制台

  在命令提示符下,需要输入node,然后回车就可以进入node在命令提示符下的交互环境了。

例子1  在nodejs的交互环境下,输入以下脚本:

var name ="zhangsan"
console.log( name );

console.log("hello nodejs");

例子2 创建一个helloworld.js,在控制台输出"hello world!".

helloworld.js

console.log('hello world');

保存该文件,并通过NodeJS 来执行。

node helloworld.js

 2 开发简单的服务器端程序

  nodeJS是遵循CommonJS规范的,既每个.js文件都是一个模块,模块的好处就是避免命名空间污染。倘若你想让一个模块对外暴露变量,可以用module.exports  =  variable;
而一个模块想要引用另一个模块对外暴露的变量,用require关键字就可以了,如var ref = require(‘module_name’);

  创建server.js,保存下列代码。

//通过require将http库包含到程序中
var http = require("http");
//创建http服务器
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World !");
response.end();
}).listen(8888);
//让服务器监听8888端口

   然后在nodejs交互环境中,输入如下命令:

node server.js

   如果一切正常,会出现如下图所示:

  然后在浏览器输入 http://localhost:8888/

  尝试在server.js编写一些常用的js函数,比如以下脚本:

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World !");
response.end();
}).listen(8888);

console.log('--- Server running! ---');

function sayHello( name){
    console.log("hello " + name);

}

setInterval(function(){
    console.log("The time is => "+new Date());
},1000);

sayHello( "zhangsan");

3 使用nodejs在输出网页

var http = require("http");

function onRequest( request, response){
        response.writeHead(200, {"Content-Type": "text/html;"  });
        response.write('<head><meta charset="utf-8"/></head>');
        response.write("Hello World123, 山东");
        response.end();
    }

http.createServer(onRequest).listen(8888);

资料参考:

http://www.runoob.com/nodejs/nodejs-tutorial.html

http://blog.csdn.net/awj3584/article/details/18401539

原文地址:https://www.cnblogs.com/wangshuo1/p/5847739.html