Node.js零起点开发(六)-处理POST请求
来源:互联网 发布:mac 浏览器兼容模式 编辑:程序博客网 时间:2024/06/10 03:09
本节我们做一个简单的demo,post数据,并且接受数据
requestHandlers的模块
应用程序需要新的部件,因此加入新的模块 -- 已经无需为此感到新奇了。我们来创建一个叫做requestHandlers的模块,并对于每一个请求处理程序,添加一个占位用函数,随后将这些函数作为模块的方法导出:
requestHandlers的模块
start() : POSt表单HTML
upload : POST数据获取页面
requestHandlers.js
function start(response, postData) { console.log("Request handler 'start' was called."); var body = '<html>'+ '<head>'+ '<meta http-equiv="Content-Type" content="text/html; '+ 'charset=UTF-8" />'+ '</head>'+ '<body>'+ '<form action="/upload" method="post">'+ '<textarea name="text" rows="20" cols="60"></textarea>'+ '<input type="submit" value="Submit text" />'+ '</form>'+ '</body>'+ '</html>'; response.writeHead(200, {"Content-Type": "text/html"}); response.write(body); response.end();}function upload(response, postData) { console.log("Request handler 'upload' was called."); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("You've sent: " + postData); response.end();}exports.start = start;exports.upload = upload;
router模块
通过检查给定的路径对应的请求处理程序是否存在,如果存在的话直接调用相应的函数,并返回相应是字符串
router.js
function route(handle, pathname, response, postData) { console.log("About to route a request for " + pathname); if (typeof handle[pathname] === 'function') { handle[pathname](response, postData); } else { console.log("No request handler found for " + pathname); response.writeHead(404, {"Content-Type": "text/plain"}); response.write("404 Not found"); response.end(); }}exports.route = route;
server模块
处理请求模块
server.js
var http = require("http");var url = require("url");function start(route, handle) { function onRequest(request, response) { var postData = ""; var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); request.setEncoding("utf8"); request.addListener("data", function(postDataChunk) { postData += postDataChunk; console.log("Received POST data chunk '"+ postDataChunk + "'."); }); request.addListener("end", function() { route(handle, pathname, response, postData); }); } http.createServer(onRequest).listen(8888); console.log("Server has started.");}exports.start = start;
index模块
启动模块,主模块
index.js
var server = require("./server");var router = require("./router");var requestHandlers = require("./requestHandlers");var handle = {}//区分大小写的handle["/"] = requestHandlers.start;handle["/start"] = requestHandlers.start;handle["/upload"] = requestHandlers.upload;server.start(router.route, handle);
运行后效果
如果现在启动应用(node index.js,始终记得这个命令行),随后请求一个URL,我请求的是http://localhost:8888/,然后显示的是一个form表单,输入数据后提交表单后就会跳转到http://localhost:8888/upload,你将会看到应用输出相应的信息,这表明我们的HTTP服务器已经在使用路由模块了,并会将请求的路径传递给路由,路由再找到对应的处理函数:
- Node.js零起点开发(六)-处理POST请求
- Node.js零起点开发(四)-路由后添加处理函数
- Node.js零起点开发(一)-NodeJs安装、Hello Word
- Node.js零起点开发(二)-require的用法
- Node.js零起点开发(三)-NodeJs路由
- node.js处理get和post请求
- Node.js零起点开发(五)-路由后添加有返回值(return)的处理函数
- Node.js零起点开发(七)-上传图片并且显示图片
- Node.js 处理post请求(表单提交)
- node.js中get与post请求的处理
- node.js 接收post请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- Node.js GET/POST请求
- n皇后问题的非递归迭代算法(C++实现)
- IT软件项目中的沟通管理
- CFE常用引导命令
- 让IOS应用从容地崩溃
- 红颜若惜的伤感爱情日志:有多少幸福,有多少甜蜜
- Node.js零起点开发(六)-处理POST请求
- 项目管理类似恋爱中感
- Ruby on Rails 入门之:(24) Ruby 中数据持久化、数据库操作
- SharedPreferences跨应用读取数据问题
- JAVA虚拟机加载类的方式解析
- 定时刷新网页上的数据
- 随机游走的期望距离
- 解密字符 I MISS YOU C++
- 队列中取最大值操作问题