Node.js 的推出,不仅从工程化的角度自动化掉更多琐碎费时的工作,更打破了前端后端的语言边界,让 JavaScript 流畅的运行在服务器端,本系列课程旨在引导前端开发工程师,以及 Node.js 初学者走进这个活泼而有富有生命力的新世界。
什么是node.js
是编写高性能服务器的JavaScript工具包
单线程,异步,事件驱动
特点,快,耗内存多
node.js性能高,开发效率高,应用范围广
node.js的安装:
下载地址:http://node.js.cn
测试下安装环境:
dos下命令行:npm
查看npm的版本:npm -v
查看node.js版本命令行:node -v
node.js是一个基于Chrome v8引擎的JavaScript运行环境,Node.js使用了一个事件驱动,非阻塞式i/o的模型,使其轻量又高效。
node.js的包管理器npm,是全球最大的开源系统。
代码语言:javascript复制var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
console.log('访问');
response.write('hello,world');
response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
代码语言:javascript复制var http = require('http');
var otherfun = require('./models/otherfuns.js');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
otherfun.controller(request,response);
otherfun.call(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//---普通函数
function fun1(res){
res.write("你好,我是fun1");
}
代码语言:javascript复制function controller(req,res){
//res.write("发送");
call('hello',req,res);
res.end("");
}
function call(res){
console.log('call');
}
module.exports = controller; //只支持一个函数
/*
//支持多个函数
module.exports={
getVisit:function(){
return visitnum ;
},
add:function(a,b){
return a b;
}
}
*/
代码语言:javascript复制var http = require('http');
var otherfun = require("./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
//fun1(response);
//-------用字符串调用对应的函数---
funname = 'fun3';
otherfun[funname](response);
//otherfun['fun3'](response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
代码语言:javascript复制module.exports = {
fun2:function(res) {
console.log("我是fun2");
},
fun3:function(res){
console.log("我是fun3");
res.write("你好");
}
}
代码语言:javascript复制var http = require('http');
var otherfun = require("./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html'}
if(request.url !== "/favicon.icon") {
otherfun(response);
response.end('');
}
}).listen(8000);
console.log('server running at http://127.0.0.1:8000/');
function fun1(res){
console.log("fun1");
res.write("hello");
}
代码语言:javascript复制var http = require('http');
var otherfun = require('./models/otherfuns.js");
http.createServer(function (request, response) {
response.writeHead(200,{'Content-Type': 'text/html'});
if(request.url !== "/favicon.ico"){
//fun1(response);
otherfun(response);
response.end('');
}
}
}).listen(8000);
function fun1(ress){
console.log("fun1");
res.write("hello");
}
代码语言:javascript复制function fun2(res){
console.log("我是fun2");
res.write("你好");
}
module.exports = fun2; // 只支持一个函数
代码语言:javascript复制module.exports = {
// 支持多个函数
fun2: function(res){
console.log("我是fun2");
res.write("你好");
},
fun3: function(res){
console.log("我是fun3");
res.write("你好");
}
}
代码语言:javascript复制if(request.url!=="/favicon.ico"){//清除第2次访问
otherfun.fun2(response);
otherfun.fun3(response);
response.end('');
}
}).listen(8000);
代码语言:javascript复制otherfun['fun2'](response);
otherfun['fun3'](response);
response.end('');
node.js调用模块
代码语言:javascript复制function User(){
this.id;
this.name;
this.age;
this.enter=function(){
console.log("进入图书馆");
}
}
模块的调用
代码语言:javascript复制var http = require('http');
// var User=require('./models/User');
var Teacher = require("./models/Teacher');
http.createServer(function (request,response) {
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){//清除第2此访问
teacher = new Teacher(1,'小红',30);
teacher.teacher(response);
response.end("");
}
}).listen(8000);
代码语言:javascript复制function User(id,name,age) {
this.id=id;
this.name=name;
this.enter=function(){
console.log("haha");
}
}
module.exports = User;
// models/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 "dashu");
}
}
module.exports = Teacher;
代码语言:javascript复制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;
代码语言:javascript复制var http = require('http');
// var User = require("./models/User");
var Teacher = require("./models/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,'dashu', 20);
teacher.enter();
teacher.teach(response);
response.end('');
}
}).listen(8000);
node.js路由:
代码语言:javascript复制var http = require('http');
var url = require('url');
var router = require('/router');
http.createServer(function(request,response){
respnse.writeHead(200, {'Content-Type': 'text/html'; charset=utf-8"});
if(request.url!=="/favicon.ico"){
var pathname=url.parse(request.url).pathname;
// console.log(pathname);
pathname = pathname.replace(///, ");// 替换掉前面的
// console.log(pathname);
router[pathname](request,response);
response.end(");
}
}).listen(8000);
module.exports={
login: function(req,res){
res.write("我是login");
}
zhuce: function(req,res){
res.write("我是注册");
}
}
读取文件:
代码语言:javascript复制var http = require('http');
var optfile=require('./models/optfile');
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");
optfile.readfile();
response.end('hello");
}
}).listen(8000);
var fs = require('fs');
module.exports = {
readfile:funciton(path){
fs.readFile(path, function(err,data){
if(err){
console.log(err);
}else{
console.log(data.toString());
}
});
console.log("dashu");
},
readfileSync:function(path){
var data=fs.readFileSync(path,'utf-8');
console.log("同步方法");
return data;
}
}