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/');​

  • 相关阅读:
    远程连接telnet和ssh的区别?(telnet如何连接)
    NFS实践(搭建页面)
    NFS挂载 卸载
    NFS实践
    03 Linux 文件管理
    02 bashshell介绍使用
    01 Linux 的渊源与发展史
    P4218 [CTSC2010]珠宝商
    P5284 [十二省联考2019]字符串问题
    广义后缀自动机(广义SAM)
  • 原文地址:https://www.cnblogs.com/xiao-zhang-blogs/p/5956323.html
Copyright © 2011-2022 走看看