【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. vue 调用常量的config.js文件

    我遇到问题,就是有很多常量需要应用的项目里面.所以需要打算设置一个config.js文件 1.填写config.js 文件 //常量配置 //快递公司名单 对应的页面为: src/pages/othe ...

  2. android上最多有多少个http连接?

    1.使用HttpUrlConnection能有几个 测试机器版本是5.1.1 个数 网络连接是否报错 写文件是否报错  1024  A/art: art/runtime/indirect_refere ...

  3. TT 安装 之 AIX

    # mkgroup -'A' id='1000' adms='root' tt -- 创建用户 # mkuser id='1000' pgrp='tt' groups='tt' adms='root' ...

  4. JMS介绍

    JMS简单描述: JMS即Java消息服务(Java Message Service),是一个Java平台中面向消息中间件的API,用于在两个应用程序之间或分布式系统中发送.接受消息,从而进行异步通信 ...

  5. jsoup: Java HTML Parser

    jsoup  Java HTML Parser jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于j ...

  6. mysqldump 命令使用

    常见选项:--all-databases, -A: 备份所有数据库--databases, -B: 用于备份多个数据库,如果没有该选项,mysqldump把第一个名字参数作为数据库名,后面的作为表名. ...

  7. easyUI--datagrid 实现按键控制( enter tab 方向键 )

    1.表格定义时加上 onClickCell: onClickCell,2.定义列时加入编辑器3.引入 key.js 即可使用 enter 键 或者向下箭头 选中单元格下移 选中单元格上移 tab键 选 ...

  8. vue之mapMutaions的使用 && vuex中 action 用法示例 && api.js的使用

    vue之mapMutations的使用 我们通过Mutation来改变store中的state,方法往往是在子组件中使用 this.$store.commit(); 来实现,但是这样的缺点是不容易查看 ...

  9. Steamworks and Unity – P2P多人游戏

    之前我们讨论过“如何把Steamworks.Net和Unity整合起来”,这是一个很好的开始,现在我们研究深一点,谈一谈Steam中的多人游戏.这不是教程,但是可以指导你在你的游戏中如何使用Steam ...

  10. 使用SeaJS实现模块化JavaScript开发【转】

    前言 SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.与jQuery等JavaScript框架不同,SeaJS不会扩展封 ...