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

posted @ 2019-01-19 15:21  会飞的斧头  阅读(2832)  评论(0编辑  收藏  举报