var http = require("http");
http.createServer(function(req,res) {
res.writeHead(200,{
"content-type":"text/plain"
});
res.write("Hello NodeJs");
res.end();
}).listen(3000);
var http = require("http");
var server = new http.Server();
server.on("request",function(req,res) {
...
}).listen(3000);
*headers/<object> - 请求头或响应头的对象 *httpVersion/<string> - http协议版本,可能的值有"1.1"或"1.0" *method/<string> - 请求的方法,例如"GET"、"POST"等 *statusCode/<number> - http响应状态码 *statusMessage/<string> - http响应状态消息 *url/<string> - 发送http请求的客户端的url
http.request(options[,callback])方法 http.request()方法返回一个http.ClientRequest的实例。其中,options是一个对象,表示请求的参数。包括: *protocol/<string> - 使用的协议,默认为http *host/<string> - 请求发送至的服务器的域名或 IP 地址,默认为"localhost" *hostname/<string> - host的别名 *port/<number> - 远程服务器的端口号,默认为80 *method/<string> - 指定http请求方法,默认为"GET" *path/<string> - 请求相对于根的路径,默认是"/",QueryString应该包含在其中。例如:/index.html?page=12 *headers/<object> - 包含请求头的对象 *timeout/<number> - 它设置了 socket 等待连接的超时时间
var http = require("http");
var querystring = require("querystring");
var postData = querystring.stringify({
"content":"测试一下",
"mid":8837
});
var options = {
hostname:"www.imooc.com",
port:80,
path:"/course/document",
method:"POST",
headers:{
"Accept":"application/json, text/javascript, */*; q=0.01",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.8",
"Connection":"keep-alive",
"Content-Length":postData.length,
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"Cookie":"",
"Host":"www.imooc.com",
"Origin":"http://www.imooc.com",
"Referer":"http://www.imooc.com/video/8837",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2763.0 Safari/537.36",
"X-Requested-With":"XMLHttpRequest",
}
}
var req = http.request(options,function(res){
res.on("data",function(chunk){
console.log(chunk);
});
res.on("end",function(){
console.log("完毕!");
});
console.log(res.statusCode);
});
req.on("error",function(err){
console.log(err.message);
})
req.write(postData);
req.end();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有