NodeJS服务器端平台实践记录

【2015 node.js learning notes】by lijun

01-note

   Nodejs是服务器端的javascript,是一种单线程、异步I/O、事件驱动型的javascript;其基本架如下:

n Node标准库(javascript实现)

n C/C++实现

u Node下层接口

u V8

u Libuv/Libio/Libev/IOCP

   NodejsCommonJS规范API的一种实现。

2-note

   Nodejs的下载、安装、测试(windows版本)、运行,npm node包管理安装supervisor

v 下载地址:https://nodejs.org  

v 安装 Nodejs 4.1.2

v 测试:win+R命令行窗口 测试指令:node ,结果:>,两次Ctrl+C即可退出

v 创建记事本编写:console.log(“this is a test for nodejs”);保存为test.js

v 命令行窗口运行test.js文件 指令: node  路径+test.js,回车即可运行

v npm安装 supervisor 指令:npm install -g supervisor

3-note

   Nodejs httpfsevents模块例子

n Http服务器:node_httpserver.js

var http=require("http");

var server=new http.Server();

server.on("request",function(req,res){

res.writeHeader(200,{"content-type":"text/html"});

res.write("<h1>Nodejs httpserver test </h1>");

res.end("<p>this is a test for nodejs httpsever</p>");

});

server.listen(3000);

console.log("nodejs httpserver is listening port 3000");

n 运行httpserver.js: node httpserver.js 浏览器地址输入http://127.0.0.1:3000

n Fs 文件系统:node_fs.js

var fs=require("fs");

function callbackF(err,data){

if(err)

console.error(err);

else

console.log("the data of test.txt:  "+data);

}

fs.readFile("node_fs.js.txt","UTF-8",callbackF);

n Events事件:node_event.js

var event=require("events");

var emiter=new event.EventEmitter();

function callbackF(param1,param2){

console.log("Name: "+param1+" password: "+param2);

}

emiter.on("myevent",callbackF);

emiter.emit("myevent","lijun","123456");

emiter.emit("myevent","admin","123456");

n 创建与加载模块

① 创建:

//mymodule.js

function hello(){

var name;

this.setName=function(pname){

name=pname;

}

this.sayhello=function(){

console.log("hello "+name);

}

}

module.exports=hello;

② 加载:

//node_testm.js

var hello=require("./node_module.js.txt");

var myhello=new hello();

myhello.setName("nodejs");

myhello.sayhello();

运行测试:node node_testm.js

n 12

4-note

   Express框架开发WEB应用

n 安装Expressnpm install -g expressnpm install -g express-generator

n 创建Express项目:express -t ejs 项目名称(当前目录创建)

n 按提示进入项目文件夹: cd 项目名称,再安装依赖:  npm install

n 按提示设置环境变量 set debug=项目名称:*,再启动npm: npm start

n 浏览器测试创建的web项目http://127.0.0.1:3000(旧版本nodejs运行项目:node app.js

n Express工程项目结构

  • app.js    项目启动及配置文件
  • package.json 项目及依赖信息
  • Views(index.ejs/jade)  视图文件夹(继承模板引擎布局的文件)
  • Views(layout.ejs/jade) 视图文件夹(模板引擎布局文件)
  • Routes(index.js) 路由控制文件夹(路由控制文件) 
  • Public 公共服务文件夹(javascripts子文件夹/images子文件夹/stylesheets子文件夹)
  • 其它

4-note   

   MongoDB非关系型数据库以及第三方Mongoose

n 下载安装新版本windows MongoDB,解压包即可完成安装

n 创建数据库文件夹命名为数据库名称dbname  如:E:mongodbdbname

n 进入E:mongodbin,设置并启动db指令:mongod  -dbpath E:mongodbdbname

Express WEB项目中连接mongodb数据库以及创建回话:

n 修改项目的package.json文件:

“denpendencies”:”mongodb”:”*”,

n 运行npm install安装依赖

n 在项目根目录创建db_setting.js

module.exports={

cookieSecret: “lijun”,

db: “mydb”,

host: ”localhost”

}

n 在项目根目录创建models文件夹,其中创建db.js

var set=require(“../db_setting”),

           Db=require(“mongodb”).Db,

   Connection=require(“mongodb”).Server,

           Server=require(“mongodb”).Server;

module.exports=new Db(set.db,new Server(se.host,Connection.DEFAULT_PORT),{safe:true});

n 创建会话

“denpendencies”:”connect-mongo”:”*”,

n 运行npm install 安装依赖

n 修改app.js文件:

var mongostore=require(“connect-mongo”)(express);

var set=require(“./db_setting ”);

//methodOveride

app.use(express.cookieParser());

app.use(express.session(){

secret:set.cookieSecret,

key:set.db,

cookie:{maxage:1000*60*60},//1 hour

store:new mongostore({

db:set.db

})

});

5-note

   MongoDB常用的基本指令:

6-note

   Nodejs的视图助手,功能为通过视图助手可访问一个全局的函数或对象。视图助手分为

① 静态视图助手:类型可以是任意类型的函数(可受参数)及对象

② 动态视图助手:类型下仅可以是函数(不可受参数),可访问 responserequest对象

   示例:

app.helpers(

inspect: function(obj){

return obj;

}

);

app.dynamicHelpers(

user: function(req,res){

     rerurn req.session.user;

}

); 

7-note

模块类型:核心模块(http/fs/net/etc)、文件模块(.json/.js/c++/c)

模块加载机制:

① 以相对路径“./模块名称(require(“./setting.js”))”或者“../模块名称(require(“../setting.js”))”方式加载模块

第一种方式require(“./setting.js”):当前文件需要加载与自己同目录的下的setting.js文件;第二方式require(“../setting.js”):当前文件需要加载与自己所在的目录X,与X目录所在目录的下的setting.文件。

② 以绝对路径方式加载:首先从当前文件所在的目录查找,再依次往上一级目录查找。

加载缓存:同一模块再次被加载时,直接从内存中加载,而不是再次把相同模块载入缓存再加载;即不会使内存重复加载已加载过的相同的模块实例。

原文地址:https://www.cnblogs.com/iTlijun/p/5129450.html