☀【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 ...
随机推荐
- 1064. Complete Binary Search Tree
二叉排序树: http://www.patest.cn/contests/pat-a-practise/1064 #include <iostream> #include <vect ...
- android.support.v4.widget.DrawerLayout使用
activity_main.xml布局如下: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas ...
- C#网络编程简单实现通信小例子-2
1.主界面 2.源代码 Client public partial class For ...
- XAML系列学习
在XAML中为属性赋值 1.使用Attribute=value形式 <Rectangle Width="100" Height="100" Stroke= ...
- SQL Server数据库备份(本机)
基础的SQL Server数据库备份存储过程 /**************************************************************************** ...
- ISoft(开源)专用下载器
继 两年的坚持,最后还是决定将ISoft开源 之后,今天再共享一款ISoft专用下载器小工具.这款工具是一年前开发的,也是一直闲置着没去扩展更新.当时开发出来就是仿穿越火线专用下载器的样式来做的,现在 ...
- objective-C 中两种实现动画的方法(转)
转发自:http://wayne173.iteye.com/blog/1250232 第一种方法: [UIView beginAnimations:@"Curl"context: ...
- java中四种引用类型
java中四种引用类型 今天看代码,里面有一个类java.lang.ref.SoftReference把小弟弄神了,试想一下,接触java已经有3年了哇,连lang包下面的类都不了解,怎么混.后来在 ...
- 团体程序设计天梯赛-练习集L1-017. 到底有多二
L1-017. 到底有多二 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个整数“犯二的程度”定义为该数字中包含2的个数与其 ...
- Ehcache详细解读(转载)
Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面 ...