zoukankan      html  css  js  c++  java
  • nodejs http post 请求带参数

    自己开发的公众号,可以领取淘宝内部优惠券

    // We need this to build our post string
    var querystring = require('querystring');
    var http = require('http');
    var fs = require('fs');
    
    function PostCode(codestring) {
      // Build the post string from an object
      var post_data = querystring.stringify({
          'compilation_level' : 'ADVANCED_OPTIMIZATIONS',
          'output_format': 'json',
          'output_info': 'compiled_code',
            'warning_level' : 'QUIET',
            'js_code' : codestring
      });
    
      // An object of options to indicate where to post to
      var post_options = {
          host: 'closure-compiler.appspot.com',
          port: '80',
          path: '/compile',
          method: 'POST',
          headers: {
              'Content-Type': 'application/x-www-form-urlencoded',
              'Content-Length': Buffer.byteLength(post_data)
          }
      };
    
      // Set up the request
      var post_req = http.request(post_options, function(res) {
          res.setEncoding('utf8');
          res.on('data', function (chunk) {
              console.log('Response: ' + chunk);
          });
      });
    
      // post the data
      post_req.write(post_data);
      post_req.end();
    
    }
    
    // This is an async file read
    fs.readFile('LinkedList.js', 'utf-8', function (err, data) {
      if (err) {
        // If this were just a small part of the application, you would
        // want to handle this differently, maybe throwing an exception
        // for the caller to handle. Since the file is absolutely essential
        // to the program's functionality, we're going to exit with a fatal
        // error instead.
        console.log("FATAL An error occurred trying to read in the file: " + err);
        process.exit(-2);
      }
      // Make sure there's data before we post it
      if(data) {
        PostCode(data);
      }
      else {
        console.log("No data to post");
        process.exit(-1);
      }
    });
    

      

  • 相关阅读:
    如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)
    NGUI系列教程六(技能冷却的CD效果)
    NGUI系列教程五(角色信息跟随)
    NGUI系列教程四(自定义Atlas,Font)
    NGUI系列教程三
    NGUI系列教程二
    NGUI系列教程一
    相机控制
    Visual Stuio 2010 常用快捷及操作
    unity3d 使用背景贴图
  • 原文地址:https://www.cnblogs.com/yehuabin/p/8193654.html
Copyright © 2011-2022 走看看