分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

nodejs http网络模块

发布时间:2023-09-06 01:16责任编辑:赖小花关键词:jsnodejshttp
基本介绍

    nodejs最重要的方面之一是具有非常迅速的实现HTTP和HTTPS服务器和服务的能力。http服务是相当低层次的,你可能要用到不同的模块,如express来实现完整的Web服务器,http模块不提供处理路由、cookie、缓存等的调用。我们主要用http模块的地方是实现供应用程序使用的后端Web服务。

代码部分

主要API:

http://nodejs.cn/api/http.html

例子:

// get/** * http get request * ?http://192.168.31.162:9001?name=001&pwd=AAAaaa */function createGetServer() { ???http.createServer(function(req, res){ ???????res.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); ???????// 解析 url 参数 ???????console.log(req.url); ???????var params = url.parse(req.url, true).query; ???????res.write("url:" + req.url + "\n"); ???????res.write("Name:" + params.name + "\n"); ???????res.write("PWD:" + params.pwd); ???????res.end(); ???}).listen(9001);}// post/** * http post request * ?http://192.168.31.162:9002?name=001&pwd=AAAaaa * ?????psot:{"aaa":"001"} */function createPostServer() { ???http.createServer(function (req, res) { ???????var params = url.parse(req.url, true).query; ???????var body = ""; ???????req.on(‘data‘, function (chunk) { ???????????body += chunk; ???????????console.log(body); ???????}); ???????req.on(‘end‘, function () { ???????????// 解析参数 ???????????// body = queryString.parse(body); ???????????// 设置响应头部信息及编码 ???????????res.writeHead(200, {‘Content-Type‘: ‘text/html; charset=utf8‘}); ???????????res.write(body); ???????????res.end(); ???????}); ???}).listen(9002);}

 

参考:

    http://nodejs.cn/api/http.html

    Node.js GET/POST请求

     Node.js Web 模块

     nodejs HTTP服务

     node/lib/_http_server.js

     node.js——http源码解读

     浅析nodejs的http模块

     NodeJS——创建最简单的HTTP服务器

 

nodejs http网络模块

原文地址:http://www.cnblogs.com/zhen-android/p/7636205.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved