zoukankan      html  css  js  c++  java
  • nodejs入门最简单例子

    一、mac话,先安装nodejs环境:

    brew install nodejs

    二、先写一个main.js

    var http = require("http");

    http.createServer(function (request, response) {

    // Send the HTTP header

    // HTTP Status: 200 : OK

    // Content Type: text/plain

    response.writeHead(200, {'Content-Type': 'text/plain'});

    // Send the response body as "Hello World"

    response.end('Hello World ');

    }).listen(3000);

    // Console will print the message

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

    192:nodejs chong$ node main.js

    Server running at http://127.0.0.1:3000/

    这时打开浏览器输入http://127.0.0.1:3000/

    三、再写一个post请求测试。

    ccy.js

    var http = require('http');

    var querystring = require('querystring');

    var contents = querystring.stringify({

    loginid:'1000005616',

    gameid:'77777777',

    productname:'ifungpth_300',

    serverid:'2001'

    });

    var options = {

    host:'localhost',

    port:7004,

    path:'/pay_callback',

    method:'POST',

    headers:{

    'Content-Type':'application/x-www-form-urlencoded',

    'Content-Length':contents.length

    }

    }

    var req = http.request(options, function(res){

    res.setEncoding('utf8');

    res.on('data',function(data){

    console.log("data:",data); //一段html代码

    });

    });

    req.write(contents);

    req.end;

    192:nodejs chong$ node ccy.js

    data: {"error":"00"}

  • 相关阅读:
    win10 ,本地连接无法识别网络 ,无线正常,
    vba 声音
    win10 优化
    比较火和常用的命令
    手机电脑平板 查图纸、查点位图、查通病、自学维修知识等通通都有的工具
    e4a mysql
    e4a 对话框的 多选单选颜色日期时间
    e4s 文本操作 数组操作
    e4a sqlite案例
    e4a-窗口切换
  • 原文地址:https://www.cnblogs.com/xingchong/p/10291831.html
Copyright © 2011-2022 走看看