欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

node.js中express模块创建服务器和http模块客户端发请求

程序员文章站 2022-05-27 13:24:49
首先下载express模块,命令行输入 npm install express 1.node.js中express模块创建服务端 在js代码同文件位置新建一个...

首先下载express模块,命令行输入

npm install express

1.node.js中express模块创建服务端

在js代码同文件位置新建一个文件夹(www_root),里面存放网页文件等,就可以在浏览器中访问了

var express = require("express");
var path = require("path");
var app = express();
//目录  (当前目录下的www_root目录)
app.use(express.static(path.join(process.cwd(),"www_root")));
//监听
var server = app.listen(6080);
app.get('/', function (req, res) {
  //发送数据
  res.send('hello world ~~~~~~~~~~~~!');
 });
// get, 处理响应
app.get("/login", function (request, respones) {
 console.log("/login comming"); 
 // 服务器收到请求后,获取客户端get操作参数
 console.log(request.query);
 // 服务器回信息给客户端
 respones.send("已连接上服务器~~");
});
app.post("/upload", function(request, respones) {
 console.log("/upload comming");
 // 获得url上传来的参数
 console.log(request.query);
 // 获得用户给我们发送过来的数据
 // 监听我们的data来获得
 request.on("data", function(data) {
 console.log(data.tostring());
 respones.send("upload ok"); 
 });
});

2.http模块客户端发请求

(实例1)http_get测试

var http = require("http");
/*
callback(is_success, data/erro)
*/
function http_get(ip, port, url, params, callback){
  //创建一个http.clientrequest对象
  var options = {
    host : ip,
    port : port,
    path : url+"?"+params,
    method : "get",
  };
  var request = http.request(options,function(incoming_msg){
    console.log("get respones");
  });
  //发送这个请求
  request.end();
}
http_get("127.0.0.1", 6080, "/login", "uname=jadeshu&upw=123456", function(is_ok,data){
});

(实例2)http_get、http_post测试

var http = require("http");
/*
 [100] = "continue",
 [101] = "switching protocols",
 [200] = "ok",
 [201] = "created",
 [202] = "accepted",
 [203] = "non-authoritative information",
 [204] = "no content",
 [205] = "reset content",
 [206] = "partial content",
 [300] = "multiple choices",
 [301] = "moved permanently",
 [302] = "found",
 [303] = "see other",
 [304] = "not modified",
 [305] = "use proxy",
 [307] = "temporary redirect",
 [400] = "bad request",
 [401] = "unauthorized",
 [402] = "payment required",
 [403] = "forbidden",
 [404] = "not found",
 [405] = "method not allowed",
 [406] = "not acceptable",
 [407] = "proxy authentication required",
 [408] = "request time-out",
 [409] = "conflict",
 [410] = "gone",
 [411] = "length required",
 [412] = "precondition failed",
 [413] = "request entity too large",
 [414] = "request-uri too large",
 [415] = "unsupported media type",
 [416] = "requested range not satisfiable",
 [417] = "expectation failed",
 [500] = "internal server error",
 [501] = "not implemented",
 [502] = "bad gateway",
 [503] = "service unavailable",
 [504] = "gateway time-out",
 [505] = "http version not supported",
}
*/
/*
callback(is_success, data/erro)
*/
// get请求的参数,是带在url的地址上面的
function http_get(ip, port, url, params, callback) {
 // step1,创建一个 http.clientrequest
 var options = {
 host: "127.0.0.1",
 port: port,
 path: url + "?" + params,
 method: "get"
 };
 // 当有请求返回的时候,参数就会被传递为http.incomingmessage
 var req = http.request(options, function(incoming_msg) {
 console.log("respones status " + incoming_msg.statuscode);
 // 监听incomingmessage的data事件,当收到服务器发过来的数据的时候,触发这个事件
 incoming_msg.on("data", function(data) {
  if (incoming_msg.statuscode === 200) {
  callback(true, data);
  }
 });
 });
 // 把这个请求发送出去
 req.end();
}
/*
http_get("127.0.0.1", 6080, "/login", "uname=blake&upwd=123456", function(is_ok, data) {
 if (is_ok) {
 console.log(data.tostring());
 }
});
*/
// post可以带body数据传到服务器
function http_post(ip, port, url, params, body, callback) {
 // step1,创建一个 http.clientrequest
 var options = {
 host: "127.0.0.1",
 port: port,
 path: url + "?" + params,
 method: "post",
 headers: {
  "content-type": "application/x-www-form-urlencoded",
  "content-length": body.length
 }
 };
 var req = http.request(options, function(incoming_msg) {
 console.log("respones status " + incoming_msg.statuscode);
 // 监听incomingmessage的data事件,当收到服务器发过来的数据的时候,触发这个事件
 incoming_msg.on("data", function(data) {
  if (incoming_msg.statuscode === 200) {
  callback(true, data);
  }
 });
 });
 // step2 写入body数据
 req.write(body);
 // 发送请求
 req.end();
}
http_post("127.0.0.1", 6080, "/upload", "filename=my_file.txt", "hello htpp post", function(is_ok, data) {
 if (is_ok) {
 console.log("upload_success", data.tostring()); 
 }
});

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接