☀【Node】处理POST请求
Node入门 √
http://www.nodebeginner.org/index-zh-cn.html
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)
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 + "'.")
})
// 监听 /start /upload 的请求,/start 没有post也有end
request.addListener("end", function() {
route(handle, pathname, response, postData)
})
}
http.createServer(onRequest).listen(8888)
console.log("Server has started.")
}
exports.start = start
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
requestHandlers.js 请求处理程序
var querystring = require("querystring")
function start(response, postData) {
console.log("Request handler 'start' was called.")
var body = '<!doctype html>' +
'<html lang="zh-CN">' +
'<head>' +
'<meta charset="utf-8">' +
'<title></title>' +
'</head>' +
'<body>' +
'<form action="/upload" method="post">' +
'<input name="title" type="text">' +
'<textarea name="description"></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) {
var body = '<!doctype html>' +
'<html lang="zh-CN">' +
'<head>' +
'<meta charset="utf-8">' +
'<title></title>' +
'</head>' +
'<body>' +
querystring.parse(postData).title +
querystring.parse(postData).description +
'</body>' +
'</html>'
console.log("Request handler 'upload' was called.")
response.writeHead(200, {"Content-Type": "text/html"})
response.write(body)
response.end()
}
exports.start = start
exports.upload = upload
☀【Node】处理POST请求的更多相关文章
- node的http请求
//node的http服务 'use strict' var http = require('http') var server = http.createServer(function (reque ...
- node后台fetch请求数据-Hostname/IP doesn't match certificate's altnames解决方法
一.问题背景 基于express框架,node后台fetch请求数据,报错Hostname/IP doesn't match certificate's altnames..... require(' ...
- mock的使用及取消,node模仿本地请求:为了解决前后端分离,用户后台没写完接口的情况下
借鉴:https://www.jianshu.com/p/dd23a6547114 1.说到这里还有一种是配置node模拟本地请求 (1)node模拟本地请求: 补充一下 [1]首先在根目录下建一个d ...
- node中间层转发请求
前台页面: $.get("/api/hello?name=leyi",function(rps){ console.info(rps); }); node中间层(比如匹配api开头 ...
- webpack4+node合并资源请求, 实现combo功能(二十三)
本文学习使用nodejs实现css或js资源文件的合并请求功能,我们都知道在一个复杂的项目当中,可能会使用到很多第三方插件,虽然目前使用vue开发系统或者h5页面,vue组件够用,但是有的项目中会使用 ...
- node.js获取请求参数的方法和文件上传
var http=require('http') var url=require('url') var qs=require('querystring') http.createServer(onRe ...
- node 发送 post 请求 get请求。
因为我们部门打算用node请求restful 然后慢慢替换掉服务端,以后直接请求soa的接口,让前端的数据更贴切项目,因为我们服务端接口和app公用一套,由于业务的需求和版本不统一(例如app6.4的 ...
- Node+Express中请求和响应对象
在用 Express 构建 Web 服务器时,大部分工作都是从请求对象开始,到响应对象终止. url的组成: 协议协议确定如何传输请求.我们主要是处理 http 和 https.其他常见的协议还有 f ...
- node.js request请求url错误:证书已过期 Error: certificate has expired
场景: node:8.9.3版本 报错代码: Error: certificate has expired at TLSSocket.<anonymous> (_tls_wrap.js:1 ...
- node.js如何处理请求的路由
var http = require( 'http' ) var handlePaths = [] /** * 初始化路由配置数组 */ function initRotute() { handleP ...
随机推荐
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- 1043. Is It a Binary Search Tree
http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...
- 因程序问题引起的服务器CPU负荷一直保持在90%以上
昨天早上刚到办公室,就接到客户的电话说其某台小型机的CPU负荷一直保持在90以上,告警短信发个不停,一直没有间断过.该服务器是一台IBM的小型机,性能应该还是不错的,出现这样的情况确实不太正常.登陆上 ...
- 【BZOJ 1070】[SCOI2007]修车
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...
- 基于Ogre的DeferredShading(延迟渲染)的实现以及应用
http://blog.sina.com.cn/s/blog_458f871201017i06.html 这篇文章写的不错,从比较宏观的角度说了下作者在OGRE中实现的延迟渲染
- dragsort拖动排序
引入文件jQuery.jquery.dragsort-0.5.2.min.js html文件: css文件: ul { margin:0px; padding:0px; margin-left:20p ...
- Notifications Nagios
Introduction I've had a lot of questions as to exactly how notifications work. This will attempt to ...
- 获取Android系统时间
目的: 输入 2014-09-09 14:02:03 输出 等待:1小时20分 注意: HH:mm:ss 为获取手机 24小时格式的时间 15:03 hh:mm:ss 为12小时模式的时 ...
- EdasStudio 开发工具用户手册
EdasStudio 开发工具用户手册 Edas 开发组2015-8-14 1. 下载安装插件 EdasStudio是EDAS的开发工具,是一个Eclipse Plugins,打开Eclipse的He ...
- Android ExpandableListView 带有Checkbox的简单应用
expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...