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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM