nodejs的学习
nodejs 环境变量的配置
1.在 安装文件下创建两个文件夹 node_global 和 node_cache
2. 执行下面命令
npm config set prefix "D:\soft\install\nodejs\node_global"
npm config set cache "D:\soft\install\nodejs\node_cache"
3. 添加系统变量:D:\soft\install\nodejs\node_global\node_modules
nodejs 就是使用js来编写服务端的程序.它的特性是(单线程 速度快 耗内存多 异步 事件驱动)
(
一些技术的解决方案:默认情况下是
1.不支持多核,可以使用cluster 进行解决
2.默认不支持服务器集群,可以使用node-http-proxy可以解决
3.可以使用nginx进行负载均衡,nginx负责静态,动态有node.js负责
4.forever或者node-cluster 实现灾难恢复
)
对框架的选择:使用express框架 超前框架使用es6语法koa sails框架
笔记
例子:
var http = require('http');
http.createServer(function(request,response){
response.writeHead(200,{'content-Type':'text/html;charset=utf-8'});
if(request.url !=="/favicon.ico"){//清除第二次访问
console.log("访问");
response.write("hello world");
response.end("访问结束"); //http 响应的结束,如果不写,则浏览器一直在转,但是会产生两次访问
}
}).listen(5555);
1.可以使用require进行模块的导入 2.方法默认导出使用,module.exports = fun2 进行方法的导出,这种只支持导出一个 3.支持多个输出,使用匿名函数. module.exports{ fun2:function(){ }, fun3:function(){ } } 4.使用字符串调用方法(otherfun.fun2()相当于otherfun['fun2']()); //这样能够灵活使用方法的调用
5.创建一个模块
function User(){
this.id;
this.name;
this.enter = function(){
console.log("都能够进入大厅")
}
}
6.导入后,可以使用上面, user = new User();
7.继承的使用
var User = require(./User);
function Teacher(id,name){
User.applay(this,[id,name])
}
8.路由require("url");属于自带的(var pathname = url.parse(request.url).pathname 可得到路由的名称,根为/,去除了端口前缀
替换前面的/,pathname = pathname.replace('/\//','');这时候,就可以和4进行结合使用了
)
9.文件的读取
var fs = require('fs');
module.exports = {
readFileSync:function(path){//同步读取数据
var data = fs.readFileSync(path,'utf-8');
console.log(data);
},
readFile:function(path){ //异步执行
fs.readFile(path,function(err,data){
if(err){
console.log(err)
}else{
console.log(data.toString());
}
})
}
}
为了避免异步请求出现的差错(因为程序没有执行完,但是已经结束的问题),一般使用闭包的形式,一般传入的为函数
recall fucntion(data){
response.write(data);
response.end("");
}
readFile:function(path,recall){ //异步执行
fs.readFile(path,function(err,data){
if(err){
console.log(err)
}else{
console.log(data.toString());
recall(data)
}
})
}
10.文件的写入
writeFileSync:function(path,data){//同步数据
var data = fs.writeFileSync(path,data);
console.log("同步文件成功");
},
writeFile:function(path,data,recall){ //异步执行
fs.writeFile(path,data,function(err){
if(err){
throw err;
}
console.log("保存成功")
recall("保存成功")
})
}
11.对图片的进行操作
图片使用二进制流的方式进行.需要设置成response.writeHead(200,{"Content-Type":"image/jpeg"});
图片的读取
readImge:function(path,response){ //异步执行
fs.readFile(path,'binary',function(err,filedata){//携带一个binary,表示二进制文件
if(err){
console.log(err)
}else{
resonse.write(filedata,'binary')
console.log(data.toString());
}
})
}
最新文章
- 从零开始学Python07作业源码:虚拟人生(仅供参考)
- MyEclipse快捷键大全
- 识别有效的IP地址和掩码并进行分类统计
- SQL for SQLite
- location of the android sdk has not been setup in the preferences
- 65. Reverse Integer &;&; Palindrome Number
- [Math] Hidden Markov Model
- Android-BaseLine基础性开发框架
- centos 下 django 1.8 配置好后 admin 后台无法显示 样式解决办法
- Android设计模式---观察者模式小demo(一)
- C#中文转换成拼音英文
- hbase权威指南学习笔记--过滤器
- vue实例的几个概念
- window安装mysql5.7解压版(解决乱码问题)
- Elasticsearch NEST 控制字段名称命名格式
- 如何不使用loop循环创建连续的数组
- 在linux系统中实现各项监控的关键技术(1)--cpu使用率的计算
- html 的实践
- 任务调度及远端管理(基于Quartz.net)
- mysql日志种类、二进制日志模式、mysqlbinlog解析二进制日志