【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

Nodejs是CommonJS规范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 http、fs、events模块例子

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: \n"+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 安装Express:npm install -g express及npm 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:\mongodb\dbname

n 进入E:\mongodb\bin\,设置并启动db指令:mongod  -dbpath E:\mongodb\dbname

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

})

});

n

5-note

MongoDB常用的基本指令:

6-note

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

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

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

示例:

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.文件。

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

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

NodeJS服务器端平台实践记录的更多相关文章

  1. ElasticSearch5.0+版本分词热更新实践记录

    前言 刚开始接触ElasticSearch的时候,版本才是2.3.4,短短的时间,现在都更新到5.0+版本了.分词和head插件好像用法也不一样了,本博客记录如何配置Elasticsearch的Hea ...

  2. Mesos+Zookeeper+Marathon的Docker管理平台部署记录(2)- 负载均衡marathon-lb

    之前介绍了Mesos+Zookeeper+Marathon的Docker管理平台部署记录(1)的操作,多余的废话不说了,下面接着说下在该集群环境下的负载均衡marathon-lb的部署过程: 默认情况 ...

  3. Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock

    在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...

  4. Spring Boot 2 实践记录之 使用 ConfigurationProperties 注解将配置属性匹配至配置类的属性

    在 Spring Boot 2 实践记录之 条件装配 一文中,曾经使用 Condition 类的 ConditionContext 参数获取了配置文件中的配置属性.但那是因为 Spring 提供了将上 ...

  5. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  6. Ionic3项目实践记录

    Ionic3首次项目实践记录 标签(空格分隔): Angular Ionic Ionic3踩坑 1. 路由懒加载(lazy load) 如果设置了懒加载,就必须全部懒加载(包括TabsPage),否则 ...

  7. k8s1.4.3安装实践记录(2)-k8s安装

    前面一篇已经安装好了ETCD.docker与flannel(k8s1.4.3安装实践记录(1)),现在可以开始安装k8s了 1.K8S 目前centos yum上的kubernetes还是1.2.0, ...

  8. Mesos, Marathon, Docker 平台部署记录

    Mesos, Marathon, Docker 平台部署记录 所有组件部署基于Ubuntu 14.04 x64 主机 IP 角色 master 192.168.1.3 Mesos Master, Ma ...

  9. Nodejs RESTFul架构实践之api篇(转)

    why token based auth? 此段摘自 http://zhuanlan.zhihu.com/FrontendMagazine/19920223 英文原文 http://code.tuts ...

随机推荐

  1. proxyee down源码分析

    proxyee down下载速度不错, 底层使用netty+多线程,最近在看netty网络方面的应用,正好这是个案例 源代码地址 https://github.com/proxyee-down-org ...

  2. 转:JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue

    从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.ThreadPoolExecutor 就是这样的线程池.以下是我的学习过程. 首先是构 ...

  3. java将list分为指定大小的新集合

    上代码: import java.util.ArrayList; import java.util.List; public class JayCommonUtil { /** * 按指定大小,分隔集 ...

  4. OpenLayers 案例一

    序 OpenLayers 是一个专为Web GIS 客户端开发提供的JavaScript 类库包,用于实现标准格式发布的地图数据访问. 例子 <!doctype html> <htm ...

  5. SpringBoot | 第三十二章:事件的发布和监听

    前言 今天去官网查看spring boot资料时,在特性中看见了系统的事件及监听章节.想想,spring的事件应该是在3.x版本就发布的功能了,并越来越完善,其为bean和bean之间的消息通信提供了 ...

  6. [转]Using NLog for ASP.NET Core to write custom information to the database

    本文转自:https://github.com/NLog/NLog/issues/1366 In the previous versions of NLog it was easily possibl ...

  7. 2017年11月4日 vs类和结构的区别&哈希表&队列集合&栈集合&函数

    类和结构的区别 类: 类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 类有构造和析构函数 类可以继承和被继承 结构: 结构是值类型在栈上分配(虽然栈的访问速度比 ...

  8. [SQL SERVER系列]工作经常使用的SQL整理,实战篇(二)[原创]

    工作经常使用的SQL整理,实战篇,地址一览: 工作经常使用的SQL整理,实战篇(一) 工作经常使用的SQL整理,实战篇(二) 工作经常使用的SQL整理,实战篇(三) 接着上一篇“工作经常使用的SQL整 ...

  9. Ubuntu真机安装

    Ubuntu真机安装 1.Ubuntu安装: (1)启动盘制作: a.下载启动盘制作工具Universal USB Installe,下载地址: b.下载Ubuntu系统镜像,到本地磁盘,官方下载地址 ...

  10. Apache Flume

    An Event is a unit of data that flows through a Flume agent. The Event flows from Source to Channel  ...