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

  • 相关阅读:
    UVa 658 (Dijkstra) It's not a Bug, it's a Feature!
    CodeForces Round #288 Div.2
    UVa 540 (团体队列) Team Queue
    UVa 442 (栈) Matrix Chain Multiplication
    CodeForces Round #287 Div.2
    CodeForces Round #286 Div.2
    CodeForces Round #285 Div.2
    UVa 12096 (STL) The SetStack Computer
    UVa 101 (模拟) The Blocks Problem
    UVa 12171 (离散化 floodfill) Sculpture
  • 原文地址:https://www.cnblogs.com/xiao-zhang-blogs/p/5956323.html
Copyright © 2011-2022 走看看