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\n');

}).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"}

转载于:https://www.cnblogs.com/xingchong/p/10291831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值