zoukankan      html  css  js  c++  java
  • Node.js 中的重要API:HTTP

    2019-12-16

    21:04:14

     

     

     

     

     

    require('http').createServer(function (req, res) {
      res.writeHead(200);
      res.end('Hello World');
    }).listen(3000);

    require('http').createServer(function (req, res) {
      res.writeHead(200,{'Content-Type':'text/html'});
      res.end('Hello <b>World</b>');
    }).listen(3000);

     

     

     

     

     

     

     

    require('http').createServer(function (req, res) {
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.end([
          '<form method="POST" action="/url">'
        ,   '<h1>My form</h1>'
        ,   '<fieldset>'
        ,   '<label>Personal information</label>'
        ,   '<p>What is your name?</p>'
        ,   '<input type="text" name="name">'
        ,   '<p><button>Submit</button></p>'
        , '</form>'
      ].join(''));
    }).listen(3000);

     

     

     

     

     

    require('http').createServer(function (req, res) {
      if ('/' == req.url) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end([
            '<form method="POST" action="/url">'
          ,   '<h1>My form</h1>'
          ,   '<fieldset>'
          ,   '<label>Personal information</label>'
          ,   '<p>What is your name?</p>'
          ,   '<input type="text" name="name">'
          ,   '<p><button>Submit</button></p>'
          , '</form>'
        ].join(''));
      } else if ('/url' == req.url && 'POST' == req.method) {
        var body = '';
    
        req.on('data', function (chunk) {
          body += chunk;
        });
    
        req.on('end', function () {
          res.writeHead(200, { 'Content-Type': 'text/html' });
          res.end('<p>Content-Type: ' + req.headers['content-type'] + '</p>'
            + '<p>Data:</p><pre>' + body + '</pre>');
        });
      }
    }).listen(3000);

     

     client.js

    require('http').request({ host: '127.0.0.1', port: 3000, url: '/', method: 'GET' }, function (res) {
      var body = '';
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
        body += chunk;
      });
      res.on('end', function () {
        console.log('
      We got: 33[96m' + body + '33[39m
    ');
      });
    }).end();

    server.js

    require('http').createServer(function (req, res) {
      res.writeHead(200);
      res.end('Hello World');
    }).listen(3000);

    server.js

    var qs = require('querystring');
    
    require('http').createServer(function (req, res) {
      var body = '';
    
      req.on('data', function (chunk) {
        body += chunk;
      });
    
      req.on('end', function () {
        res.writeHead(200);
        res.end('Done');
    
        console.log('
      got name 33[90m' + qs.parse(body).name + '33[39m
    ');
      });
    }).listen(3000);

    client.js

    var http = require('http')
      , qs = require('querystring')
    
    function send (theName) {
      http.request({ host: '127.0.0.1', port: 3000, url: '/', method: 'POST' }, function (res) {
        res.setEncoding('utf8');
        res.on('end', function () {
          console.log('
      33[90m✔ request complete!33[39m');
          process.stdout.write('
      your name: ');
        });
      }).end(qs.stringify({ name: theName }));
    }
    
    process.stdout.write('
      your name: ');
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    process.stdin.on('data', function (name) {
      send(name.replace('
    ', ''));
    });

     

  • 相关阅读:
    js post 异步请求
    Android 实现文件上传功能(upload)
    js 金额文本框实现代码
    纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等)
    NBearV2视频教学系列总索引,欢迎多提意见和建议[09/21更新至IoC篇]
    1.2 实体实例化及使用自定义实体[发布时间:9/6]
    NBear视频 4.1 基于NBear.IoC的企业级系统构架[发布时间:9/21]
    2.1 基于NBear.Data的实体持久化[发布时间:9/10]
    NBear案例源码 简易AJAX留言板 [Updated 10/31 Powered by NBear V3.0.0 preview]
    全面解析ASP.NET2.0下的URL重写
  • 原文地址:https://www.cnblogs.com/JasonPeng1/p/12052477.html
Copyright © 2011-2022 走看看