首页 > Web开发 > 详细

nodejs

时间:2017-02-24 18:09:43      阅读:278      评论:0      收藏:0      [点我收藏+]

nodeJS

1.安装nodejs
从nodejs官网下载最新版本的node,设置环境变量这样就可以在cmd下直接用命令行操作npm
环境变量:path  d:/nodejs
查看本机node及npm版本
技术分享
 
2.从官网上直接拷一个小脚本:
新建nodeExample.js
const http = require(‘http‘);
const hostname = ‘127.0.0.1‘;
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader(‘Content-Type‘, ‘text/plain‘);
  res.end(‘Hello World\n‘);
});
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

可以通过控制台直接运行:

技术分享
打开浏览器输入http://127.0.0.1:3000/,页面上出现Hello World
如果将Hello World改成Hello NodeJs,刷新浏览器发现页面还是没变,此时要再次在控制台再次运行该js文件,Ctrl+C结束上次活动
 
函数篇
看一个本地函数nodeExample.js
var http=require(‘http‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        fun1(response);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
function fun1(res){
    console.log(‘fun1‘);
    res.write("hello,this is fun1");
}
如果是别的地方引用的函数
新建function2.js
function fun2(res){
    console.log(‘fun2‘);
    res.write(‘hello,this is fun2‘);
}
module.exports=fun2;

nodeExample.js修改为

var http=require(‘http‘);
var otherFun=require("./function2.js");
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        fun1(response);
        otherFun(response);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
function fun1(res){
    console.log(‘fun1‘);
    res.write("hello,this is fun1");
}

控制台使用node命令执行nodeExample.js

技术分享
技术分享
支持多个函数:
function2.js
module.exports={
    fun2:function(res){
        console.log(‘fun2‘);
        res.write(‘hello,this is fun2‘);
    },
    fun3:function(res){
        console.log(‘fun3‘);
        res.write(‘hello,this is fun3‘);
    }
}

nodeExample.js

var http=require(‘http‘);
var otherFun=require("./function2.js");
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        otherFun.fun2(response);
        otherFun.fun3(response);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
技术分享
调用时也可以这样:otherFun[‘fun3‘](response);
也可以这样:
funname=‘fun3‘;
otherFun[funname ](response);
 
调用模块
创建User.js
function User(id,name,age){
    this.id=id;
    this.name=name;
    this.age=age;
    this.enter=function(){
        console.log(this.name+‘进入图书馆‘);
    }
}
module.exports=User;

nodeExample.js

var http=require(‘http‘);
var User=require("./User.js");
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        user=new User(1,"张三",20);
        user.enter();
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);

运行,再增加Teacher.js

var User=require(‘./User‘);
function Teacher(id,name,age){
    User.apply(this,[id,name,age]);
    this.teach=function(res){
        res.write(this.name+"讲课");
    }
}
module.exports=Teacher;

nodeExample.js

var http=require(‘http‘);
var Teacher=require("./Teacher");
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        teacher=new Teacher(1,"张三",20);
        teacher.enter();
        teacher.teach(response);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
技术分享
 
 
路由
var http=require(‘http‘);
var url=require(‘url‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        console.log(pathname);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
在浏览器在Url地址后加上login,结果:
技术分享
如果不想要/,可以通过正则去掉
var http=require(‘http‘);
var url=require(‘url‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        //替换掉前面的/
        pathname=pathname.replace(/\//,‘‘);
        console.log(pathname);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
 
 
新建route.js
module.exports={
    login:function(req,res){
        res.write("this is login method!");
    },
    zhuce:function(req,res){
        res.write("This  is zhuce method");
    }
}

nodeExample.js

var http=require(‘http‘);
var url=require(‘url‘);
var route=require(‘./route‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        //替换掉前面的/
        pathname=pathname.replace(/\//,‘‘);
        console.log(pathname);
        route[pathname](request,response);
        response.end();
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
技术分享
如果浏览器上输入其他url地址,后台就会报错:
技术分享
 
 
读文件
同步方式读取文件
创建openFile.js
var fs=require(‘fs‘);
module.exports={
    readFileSync:function(path){
        var data=fs.readFileSync(path,‘utf-8‘);
        console.log(data);
        console.log("同步方法执行完毕");
    }
}

nodeExample.js

var http=require(‘http‘);
var openFile=require(‘./openFile‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        //这里的path是相对于openFile.js路径的
        openFile.readFileSync(‘./login.html‘);
        response.end();
        console.log(‘主程序执行完毕‘);
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
在跟openFile.js文件相同的路径下创建login.html,该html中只有 登录成功 四个字
在控制台执行命令:
技术分享
 
异步方式读取文件
openFile.js修改为:
var fs=require(‘fs‘);
module.exports={
    readFile:function(path){
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
            }
        });
        console.log("异步方式执行完毕");
    }
}

nodeExample.js

var http=require(‘http‘);
var openFile=require(‘./openFile‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        openFile.readFile(‘./login.html‘);
        response.end(‘ok‘);
        console.log(‘主程序执行完毕‘);
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
也就是说,程序执行到fs.readFile()这个函数时,这个函数还没有执行完,程序自动往下走,先读出了console.log("异步方式执行完毕");
如果将程序openFile.js改为:
var fs=require(‘fs‘);
module.exports={
    readFile:function(path,res){
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                res.write(data);
            }
        });
        console.log("异步方式执行完毕");
    }
};
nodeExample.js相应的修改函数:openFile.readFile(‘./login.html‘,response);
技术分享
主要的问题是,openFile.readFile(‘./login.html‘,response);这个函数还没有执行完,response.end(‘ok‘);就已经执行完了
可以用闭包开解决:
nodeExample.js
var http=require(‘http‘);
var openFile=require(‘./openFile‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;chaeset=utf-8‘});
    if(request.url!="/favicon.ico"){
        function recall(data){
            response.write(data);
            response.end(‘ok‘);
        }
        openFile.readFile(‘./login.html‘,recall);
        console.log(‘主程序执行完毕‘);
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
openFile.js
var fs=require(‘fs‘);
module.exports={
    readFile:function(path,recall){
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                recall(data);
            }
        });
        console.log("异步方式执行完毕");
    }
};
执行过程:执行到openFile.readFile()函数时,开个内存执行该函数,另一个继续往下执行函数之后的程序,先把能执行的给执行,不能立刻执行的继续执行
技术分享
 
 
将之前的路由修改为:
nodeExample.js
var http=require(‘http‘);
var url=require(‘url‘);
var route=require(‘./route.js‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        pathname=pathname.replace(/\//,‘‘);
        route[pathname](request,response);
    }
}).listen(8000);
console.log(‘server running at http://127.0.0.1:8000/‘);
route.js
var openFile=require(‘./openFile‘);
module.exports={
    login:function(req,res){
        function recall(data){
            res.write(data);
            res.end();
        }
        openFile.readFile(‘./login.html‘,recall);
    }
}
openFile.js
var fs=require(‘fs‘);
module.exports={
    readFile:function(path,recall){
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                recall(data);
            }
        });
        console.log("异步方式执行完毕");
    }
};
技术分享
技术分享
程序首先得到了pathname,如login,转到login函数,执行openFile.readFile()函数,将login.html读取出来,并将读取的内容通过recall函数写出来
 
写文件
也有同步跟异步两种方式
新增writeFile.js
var fs=require(‘fs‘);
module.exports={
    writeFile:function(path,data,recall){
        fs.writeFile(path,data,function(err){
            if(err){
                throw err;
            }
            console.log(‘This file is saved‘);
            recall(‘write file success‘);
        });
    },
    writeFileSync:function(path,data){
        fs.writeFileSync(path,data);
        console.log(‘同步写文件成功‘);
    }
}
route.js
var openFile=require(‘./openFile‘);
var writeFile=require(‘./writeFile‘)
module.exports={
    login:function(req,res){
        function recall(data){
            res.write(data);
            res.end();
        }
        openFile.readFile(‘./login.html‘,recall);
    },
    writeFile:function(req,res){
        function recall(data){
            res.write(data);
            res.end();
        }
        writeFile.writeFile(‘./one.txt‘,‘我的写入文件‘,recall)
    }
}
nodeExample.js
var http=require(‘http‘);
var url=require(‘url‘);
var route=require(‘./route.js‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘text/html;chaeset=utf-8‘});
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        pathname=pathname.replace(/\//,‘‘);
        route[pathname](request,response);
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
技术分享
技术分享
成功之后打开one.txt可以看到    我的写入文件
 
 
读取图片
nodeExample.js
var http=require(‘http‘);
var openFile=require(‘./openFile.js‘);
http.createServer(function(request,response){
    response.writeHead(200,{‘Content-Type‘:‘image/jpeg‘});
    if(request.url!="/favicon.ico"){
        openFile.readImg(‘./image.png‘,response);   
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
openFile.js
var fs=require(‘fs‘);
module.exports={
    readImg:function(path,res){
        fs.readFile(path,‘binary‘,function(err,file){
            if(err){
                console.log(err);
                return;
            }else{
                console.log(‘输出文件‘);
                res.write(file,‘binary‘);
                res.end();
            }
        })
    }
};
技术分享
技术分享
如果加上文字,就会报错,如:
if(request.url!="/favicon.ico"){
        response.write(‘hello world‘);
        openFile.readImg(‘./image.png‘,response);   
    }
此时图片就无法显示
 
 
再写路由的一个例子,解决这个问题
nodeExample.js
var http=require(‘http‘);
var url=require(‘url‘);
var route=require(‘./route.js‘);
http.createServer(function(request,response){
    if(request.url!="/favicon.ico"){
        var pathname=url.parse(request.url).pathname;
        pathname=pathname.replace(/\//,‘‘);
        route[pathname](request,response);
    }
}).listen(8000);
console.log(‘serer running at http://127.0.0.1:8000/‘);
route.js
var openFile=require(‘./openFile‘);
module.exports={
    login:function(req,res){
        res.writeHead(200,{‘Content-Type‘:‘text/html;charset=utf-8‘});
        function recall(data){
            res.write(data);
            res.end();
        }
        openFile.readFile(‘./login.html‘,recall);
    },
    showImg:function(req,res){
        res.writeHead(200,{‘Content-Type‘:‘image/jpeg‘});
        openFile.readImg(‘./image.png‘,res);
    }
}
openFile.js
var fs=require(‘fs‘);
module.exports={
    readFile:function(path,recall){
        fs.readFile(path,function(err,data){
            if(err){
                console.log(err);
            }else{
                console.log(data.toString());
                recall(data);
            }
        });
        console.log("异步方式执行完毕");
    },
    readImg:function(path,res){
        fs.readFile(path,‘binary‘,function(err,file){
            if(err){
                console.log(err);
                return;
            }else{
                console.log(‘输出文件‘);
                res.write(file,‘binary‘);
                res.end();
            }
        })
    }
};
login.html
<html>
<head>
</head>
<body>
登录界面
<img src="./showImg">
</body>
</html>
技术分享
技术分享
 
 
 
 
 

nodejs

原文:http://www.cnblogs.com/lyy-2016/p/6439559.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!