zoukankan      html  css  js  c++  java
  • nodejs创建一个HTTP服务器 简单入门级

    const http = require('http');//请求

    http.createServer(function(request, response){
        /*createServer该函数用来创建一个HTTP服务器,并将 requestListener 作为 request 事件的监听函数
        由于该方法属于http模块,使用前需要引入http模块 (http = require('http'))
        http.createServer([requestListener])
        response指定了响应头,响应体内容为node.js,用end结束。
        最后调用listen函数,监听8124端口。*/
      response.writeHead(200, {'Content-Type': 'text/plain'});
      /*响应向请求的客户端发送响应头。该函数在一个请求内最多只能调用一次,
      如果不调用,则会自动生成一个响应头。
      statusCode  HTTP状态码,如200(请求成功),404(未找到)等。
      reasonPhrase
      headers  类似关联数组的对象,表示响应头的每个属性*/
      response.write('Node.js');
      /*write向请求的客户端发送响应内容
      在 response.end() 之前,response.write() 可以被执行多次。
      response.write(chunk, [encoding])
      chunk 是一个buffer 或 字符串,表示发送的内容
      encoding 如果chunk是字符串,就需要指定encoding来说明它的编码方式,默认utf-8*/
      response.end('Hello World ');
    }).listen(8124);

    console.log('Server running at http://127.0.0.1:8124/');​

  • 相关阅读:
    SQL Server 日期和时间函数
    sql日期格式化函数
    C#中事件的使用
    CSS中的display:inline-block
    用aspnet_regiis注册Framework4.0框架
    什么是CSS hack
    第一次MySQL的SQL注入实验
    (二分)Block Towers(cf626)
    (多线程dp)Matrix (hdu 2686)
    (数位dp)Bomb (hdu 3555)
  • 原文地址:https://www.cnblogs.com/xiao-zhang-blogs/p/5956323.html
Copyright © 2011-2022 走看看