2016-08-09学习笔记

nodejs静态资源服务器

1. touch 一般是在创建一个空的日志文件的时候才会使用
2. xshell 使用open命令来打开一个文件
3. http是nodejs的服务模块
4. url是url路由模块
5. fs是文件服务器模块

nodejs实例

打开xshell 打开ubuntu 用xshell链接ubuntu

1. nodejs服务器的创建:

const http=require('http');
const ip = '本机IP地址';
const port = 3000;
http.createServer((req,res)=>{
    res.writeHead(200,{'Content-type':'text/html'});
    res.write('hello');
    res.end();
}).listen(port,ip,()=>{
      console.log('server start');
});
//主机IP
const ip = '192.168.0.110';
//端口号
const port = 3000;
//引入的组建模块  http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');

//创建一个服务
var server = http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/plain'});
     res.write('my nodejs');
    res.end();
});
//监听一个端口
server.listen(port,ip,function(){
    console.log('server start');
});
//封装成函数调用实例
const http=require('http');
const ip = '本机IP地址';
const port = 3000;
var f = function(req,res){
  res.writeHead(200,{'Content-type':'text/html'});
  res.write('hello');
  res.end();
}
http.createServer(f).listen(port,ip,()=>{
  console.log('server start');
});

2. 抽象方法及获取URL部分块的内容 url

const ip = '192.168.0.110';//主机IP
const port = 3000;//端口号
//引入的组建模块  http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');

//创建服务的回掉函数
var funSer = function(req,res){
    //获取url地址块的内容  如:/path/show
    var parth = url.parse(req.url).pathname;
res.write(parth);
res.end();
}

//监听端口的回掉
var fun = function(){
console.log('server start');
}

var server = http.createServer(funSer).listen(port,ip,fun);
const http=require('http');
const url = require('url');
const ip = '本机IP地址';
const port = 3000;
var f = function(req,res){
  var pathname = url.parse(req.url).pathname;
  res.write(pathname);
  res.end();
}
var f2 = function(){
  console.log('server start');
}
http.createServer(f).listen(port,ip,f2);

3. 读取文件的内容 File System

const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本机IP地址';
const port = 3000;

var pathname = url.parse(req.url).pathname;
var userurl = url.parse(pathname);

switch(userurl){
  case '' || '/':
      fs.readFile('./index.html',func(err,content){
        if(err){
          console.log(err);
        }
        else{
          res.writeHead(200,{'Content-type':'text/html'});
        }
      });
}
const ip = '192.168.0.110';//主机IP
const port = 3000;//端口号
//引入的组建模块  http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');

//真正打印文件内容
fs.readFile('./index.html', (err, data) => {
    if (err) throw err;
    //打印字符串内容
    console.log(data.toString());
});

//创建服务的回掉函数
var funSer = function(req,res){
    //获取url地址块的内容  如:/path/show
    var parth = url.parse(req.url).pathname;
    res.write(parth);
    res.end();
}

//监听端口的回掉
var fun = function(){
    console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);

4. 读取文件的内容并在浏览器中输出 File System

const ip = '192.168.0.110';//主机IP
const port = 3000;//端口号
//引入的组建模块  http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');

//读取文件内容
var data = fs.readFileSync('./index.html');
//创建服务的回掉函数
var funSer = function(req , res){
//获取url地址块的内容  如:/path/show
var path = urls.parse( req.url ).pathname;
res.write( data.toString() );
res.write( path );
res.end();
}

//监听端口的回掉
var fun = function(){
console.log('server start');
}
var server = http.createServer(funSer).listen(port,ip,fun);

5. 完整实例(根据不同的url地址请求不同的文件【模板】)

const ip = '192.168.0.110';//主机IP

const port = 3000;//端口号

//引入的组建模块  http、url、fs
const http = require('http');
const url = require('url');
const fs = require('fs');

//实例化一个服务容器
var server = new http.Server();

//监听一个端口
server.listen(port , ip);

//注册一个事件处理的on方法
server.on('request' , function(request , response){
  
//解析请求的url
var url = urlapi.parse(request.url);

//监听请求的网站,以当前脚本目录为根目录的url地址
console.log(url.pathname);

//特殊URL会让服务器在发送响应前先等待
//根据path路径来读取不同的模板文件
switch( url.pathname ){ //判断请求的路径信息
    case '' || '/': //处理请求网站根目录,指定加载对应的文件夹,一般以根目录的index.html为默认,nodejs是高效流处理的方案,也可以通过配置文件来设置
    
        //读取文件内容(打开请求的文件)
        fs.readFile('./index.html',function( error, content){
            if(error){//如果有错误时,显示错误信息
                res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
                res.write(error.message);
                res.end();
            }else{
                //正确时浏览器输出模板文件的内容
                res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});//告诉相应的请求头信息,返回数据
                res.write(content);//模板文件内容
                res.end();
            }
        });
        break;
    case '/list':
        fs.readFile('./list.html',function( error, content){
            if(error){
                res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
                res.write(error.message);
                res.end();
            }else{
                res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
                res.write(content);
                res.end();
            }
        });
        break;
    case '/show':
        fs.readFile('./show.html',function( error, content){
            if(error){
                res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
                res.write(error.message);
                res.end();
            }else{
                res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
                res.write(content);
                res.end();
            }
        });
        break;
    default:
        fs.readFile('./default.html',function( error, content){
            if(error){
                res.writeHead(400,{'Content-Type':'text/plain;charset="utf-8"'});
                res.write(error.message);
                res.end();
            }else{
                res.writeHead(200,{'Content-Type':'text/html;charset="utf-8"'});
                res.write(content);
                res.end();
            }
        });
        break;

    }

});

练习:http 文件(每次都需要重启服务器)

const http=require('http');
const ip = '本机IP地址';
const port = 3000;
http.createServer((req,res)=>{
    res.writeHead(200,{'Content-type':'text/html'});
    res.write('hello');
    res.end();
}).listen(port,ip,()=>{
      console.log('server start');
});
const http=require('http');
const ip = '本机IP地址';
const port = 3000;
var f = function(req,res){
  res.writeHead(200,{'Content-type':'text/html'});
  res.write('hello');
  res.end();
}
http.createServer(f).listen(port,ip,()=>{
  console.log('server start');
});
const http=require('http');
const ip = '本机IP地址';
const port = 3000;
var f = function(req,res){
  res.writeHead(200,{'Content-type':'text/html'});
  res.write('hello');
  res.end();
}
var f2 = function(){
  console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
const http=require('http');
const url = require('url');
const ip = '本机IP地址';
const port = 3000;
var f = function(req,res){
  var pathname = url.parse(req.url).pathname;
  res.write(pathname);
  res.end();
}
var f2 = function(){
  console.log('server start');
}
http.createServer(f).listen(port,ip,f2);
const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本机IP地址';
const port = 3000;

var data = fs.readFileSync('xiaoniu.txt');
var f = function(req,res){
  var pathname = url.parse(req.url).pathname;
  res.write(pathname);
  res.end();
}
var f2 = function(){
  console.log('server start');
}
http.createServer(f).listen(port,ip,f2);  
const http=require('http');
const fs = require('fs');
const url = require('url');
const ip = '本机IP地址';
const port = 3000;

var pathname = url.parse(req.url).pathname;
var userurl = url.parse(pathname);

switch(userurl){
  case '' || '/':
      fs.readFile('./index.html',func(err,content){
        if(err){
          console.log(err);
        }
        else{
          res.writeHead(200,{'Content-type':'text/html'});
        }
      });
}

参考资料:http://cnodejs.org/topic/4f16442ccae1f4aa27001071

你可能感兴趣的:(2016-08-09学习笔记)