参考:http://www.runoob.com/nodejs/nodejs-http-server.html
1.下载并解压(直接用已编译好的包安装)
# wget https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz --如果这里下载不成功的话,用下面的命令
# wget --no-check-certificate https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz
# tar xf node-v10.9.0-linux-x64.tar.xz -C /usr/local/ --解压到 /usr/local/目录下
# cd /usr/local/node-v10.9.0-linux-x64/ --进入解压后的目录
# ./bin/node -v --查看版本,校验是否安装成功
2.给bin目录下的node npm 等命令设置软连接:设置软连接的方法 ln -s xxx1 xxx2用快捷方式xxx2命令代替了xxx1(类似与window的快捷键)
# ln -s /usr/local/node-v10.9.0-linux-x64/bin/node /usr/local/bin/
# ln -s /usr/local/node-v10.9.0-linux-x64/bin/npm /usr/local/bin/
# node -v --设置了软连接后,在其他目录下也能直接用 node 命令
3.设置防火墙放行8888端口
# vim /etc/sysconfig/iptables -- 打开端口配置文件,复制22端口所在行数据,并将22改成8888(linux默认只放行22端口)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8888 -j ACCEPT --将这行代码添加到22所在行的下面
:wq -- 退出并保存
# service iptables restart --重启防火墙,使端口配置生效
4.编写第一个helloworld
# node /home/hello.js --用node 运行 hello.js 文件 在浏览器访问验证是否成功
5.使用 npm 安装模块
# npm install express --本地安装:将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录
# npm install express -g --全局安装:将安装包放在 /usr/local 下或者你 node 的安装目录
通过 var express = require("express") 就可以引用安装好的模块
# npm uninstall express --卸载模块
# npm ls --查看是否已卸载
# npm update express --更新模块
# npm search express --搜索模块
6.package.json 的使用:用户定义包的属性
name - 包名
version - 包的版本号
description - 包的描述
homepage - 包的官网 url
author - 包的作者姓名
dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。
。。。
7.异步回调函数 function foo1(name, age, callback) { } 或 function foo2(value, callback1, callback2) { }
var fs = require("fs"); --文件模板 var data = fs.readFileSync('input.txt'); --同步读取文件(等这里执行完才会执行下面的语句) console.log(data.toString()); console.log("程序执行结束!"); -- 这里会先输出文件内容,再输输出“程序执行结束”
var fs = require("fs"); fs.readFile('input.txt', function (err, data) { --异步读取文件(这里在读取时,程序还会往下走) if (err) return console.error(err); console.log(data.toString()); }); console.log("程序执行结束!"); --这里会先输出“程序执行结束”,再输出文件内容
8. node 的事件模块
// 引入 events 模块 var events = require('events'); // 创建 eventEmitter 对象 var eventEmitter = new events.EventEmitter(); // 创建事件处理程序 var connectHandler = function connected() { console.log('连接成功。'); // 触发 data_received 事件 eventEmitter.emit('data_received'); } // 绑定 connection 事件处理程序 eventEmitter.on('connection', connectHandler); // 使用匿名函数绑定 data_received 事件 eventEmitter.on('data_received', function(){ console.log('数据接收成功。'); }); // 触发 connection 事件 eventEmitter.emit('connection'); console.log("程序执行完毕。");
9. Node.js模块系统:模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的。换言之,一个 Node.js 文件就是一个模块。Node.js 提供了 exports 和 require 两个对象,其中 exports 是模块公开的接口,require 用于从外部获取一个模块的接口,即所获取模块的 exports 对象。
//hello.js function Hello() { var name; this.setName = function(thyName) { name = thyName; }; this.sayHello = function() { console.log('Hello ' + name); }; }; module.exports = Hello;
//main.js var Hello = require('./hello'); --引入了当前目录下的 hello.js 文件(./ 为当前目录,node.js 默认后缀为 js) hello = new Hello(); --这里需要创建 Hello 的对象,才能调用hello.js里面的属性 hello.setName('BYVoid'); hello.sayHello();