Node.js创建简单的http服务

/*
  使用Node构建一个简单的Web服务器,在Node中专门提供了一个核心模块:http,用来构建编写服务器
*/
//加载核心模块
var http =  require('http');

//使用 http.createServer() 的方法创建一个 Web 服务器 , 返回一个Server实例
var server = http.createServer();

/*
   服务器用处:
      1.提供服务: 对数据的服务
      2.发请求
      3.接收请求
      4.处理请求
      5.发送响应(反馈)
*/
//客户端的请求会触发服务器的 request 请求事件,然后执行第二个参数:回调函数
server.on('request',function(){
    
    
    console.log('Had accepted the request from client.');
});

//绑定端口号
server.listen(8000,function(){
    
    
    console.log('The server has been started, you can access the server through the ip——http://127.0.0.1:3000');
});
var http = require('http');

var Server = http.createServer();

/*
 request请求事件处理函数,需要接收两个参数:  
     Request: 请求对象
         请求对象可以用来获取客户端的一些请求信息,例如请求路径;
    Response: 响应对象
        *响应对象可以用来给客户端发送响应信息
        *response对象有个方法: write (用来给客户端发送响应数据)
         但最后一定要用end来结束响应,否则客户端会一直等待
 */
Server.on('request',function(request,response){
    
    
     // http: //127.0.0.1:3000/vodka/xxxxxx // url
     console.log('Had accepted the request from client and the url: ' + request.url);
     //发送响应数据
     response.write('Hello Guys!');
     response.write('Welcome to my website!');
     //通知客户端,服务端的数据已经发送完毕,客户端可以呈现数据用户了
     response.end();
});

Server.listen('3000',function(){
    
    
    console.log('Server has been started, you can access it from ip: http: //127.0.0.1:3000/');
});
var http = require('http');

var Server = http.createServer();

/*
 request请求事件处理函数,需要接收两个参数:  
     Request: 请求对象
         请求对象可以用来获取客户端的一些请求信息,例如请求路径;
    Response: 响应对象
        *响应对象可以用来给客户端发送响应信息
        *response对象有个方法: write (用来给客户端发送响应数据)
         但最后一定要用end来结束响应,否则客户端会一直等待
 */
Server.on('request',function(request,response){
    
    
     // http: //127.0.0.1:3000/vodka/xxxxxx // url
     console.log('Had accepted the request from client and the url: ' + request.url);
     //根据不同url,发送不同的响应数据
     switch(request.url){
    
    
         case '/name': response.write('Vodka!'); break;
         case '/login': response.write('register~'); break;
         case '/hahaha': response.write('laughing~'); break;
     }
     //通知客户端,服务端的数据已经发送完毕,客户端可以呈现数据用户了
     response.end();
});

Server.listen('3000',function(){
    
    
    console.log('Server has been started, you can access it from ip: http: //127.0.0.1:3000/');
});

从服务器返回一个数组,数组中包含对象和其他属性

var http = require('http');

var server = http.createServer();
//监听 request 请求事件,设置请求处理函数
server.on('request',function(request,response){
    
    
    
    if(request.url === '/objects'){
    
    
        var Objects = [
        {
    
    
            name: 'Apple',
            price: '89-dollars',
            info: {
    
    
               address: 'China',
               Date: '2021-9-30'
            }
        },
        {
    
    
            name: 'Apple',
            price: '89-dollars',
            info: {
    
    
               address: 'China',
               Date: '2021-9-30'
            }
        },
        {
    
    
            name: 'Apple',
            price: '89-dollars',
            info: {
    
    
               address: 'China',
               Date: '2021-9-30'
            }
        },
        {
    
    
            name: 'Apple',
            price: '89-dollars',
            info: {
    
    
               address: 'China',
               Date: '2021-9-30'
            }
        }
    ];
        var products = JSON.stringify(Objects);
        //响应内容的类型:
    /*
      在服务端默认发送的数据,是 utf8编码的内容,但浏览器并不知情,因此浏览器会按照当前操作系统的默认编码去解析,
      中文操作系统默认是 gbk 
      解决方法: 让浏览器知道发送的内容编码是什么
   */
       response.setHeader('Content-type','text/plain; charset=utf-8');
 
   var url = request.url;
   //普通文本
   if(url === 'plain'){
    
    
      response.setHeader('Content-Type','text/plain; charset = utf-8');
      response.end('hello');
   }else if(url === 'html'){
    
    
      //html文本
      rsponse.setHeader('Content-Type','text/html; charset = utf-8');
      response.end('<p>hello</p>')
   }
 
        response.end(products);
        console.log('发送该请求的客户端的ip地址和端口号: ' ,request.socket.remoteAddress,request.socket.remotePort);
    }
});
//服务器绑定端口号,启动服务器
server.listen(3000,function(){
    
    
   console.log('You can access the website from ip——http:127.0.0.1:3000');
});
//ip地址用来定位计算机,端口号用来定位具体的应用程序,所有需要通信联网的软件都有端口号

   

猜你喜欢

转载自blog.csdn.net/Vodka688/article/details/114903479
今日推荐