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 @   会飞的斧头  阅读(2836)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示