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请求的更多相关文章

  1. node的http请求

    //node的http服务 'use strict' var http = require('http') var server = http.createServer(function (reque ...

  2. node后台fetch请求数据-Hostname/IP doesn't match certificate's altnames解决方法

    一.问题背景 基于express框架,node后台fetch请求数据,报错Hostname/IP doesn't match certificate's altnames..... require(' ...

  3. mock的使用及取消,node模仿本地请求:为了解决前后端分离,用户后台没写完接口的情况下

    借鉴:https://www.jianshu.com/p/dd23a6547114 1.说到这里还有一种是配置node模拟本地请求 (1)node模拟本地请求: 补充一下 [1]首先在根目录下建一个d ...

  4. node中间层转发请求

    前台页面: $.get("/api/hello?name=leyi",function(rps){ console.info(rps); }); node中间层(比如匹配api开头 ...

  5. webpack4+node合并资源请求, 实现combo功能(二十三)

    本文学习使用nodejs实现css或js资源文件的合并请求功能,我们都知道在一个复杂的项目当中,可能会使用到很多第三方插件,虽然目前使用vue开发系统或者h5页面,vue组件够用,但是有的项目中会使用 ...

  6. node.js获取请求参数的方法和文件上传

    var http=require('http') var url=require('url') var qs=require('querystring') http.createServer(onRe ...

  7. node 发送 post 请求 get请求。

    因为我们部门打算用node请求restful 然后慢慢替换掉服务端,以后直接请求soa的接口,让前端的数据更贴切项目,因为我们服务端接口和app公用一套,由于业务的需求和版本不统一(例如app6.4的 ...

  8. Node+Express中请求和响应对象

    在用 Express 构建 Web 服务器时,大部分工作都是从请求对象开始,到响应对象终止. url的组成: 协议协议确定如何传输请求.我们主要是处理 http 和 https.其他常见的协议还有 f ...

  9. node.js request请求url错误:证书已过期 Error: certificate has expired

    场景: node:8.9.3版本 报错代码: Error: certificate has expired at TLSSocket.<anonymous> (_tls_wrap.js:1 ...

  10. node.js如何处理请求的路由

    var http = require( 'http' ) var handlePaths = [] /** * 初始化路由配置数组 */ function initRotute() { handleP ...

随机推荐

  1. 《C和指针》 读书笔记 -- 第13章 高级指针话题

    1.函数指针 int (*f)(); int *(*f[])(); 用途: [1]回调函数 e.g. /*在一个单链表中查找指定值*/ Node *search_list(Node *node,voi ...

  2. dom树的介绍,及原理分析

    三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...

  3. nodejs的模块系统(实例分析exprots和module.exprots)

    前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...

  4. 十六、mysql 分区之 简单sql优化2

    .索引的分类 B-Tree 基本支持 HASH 只有memory支持 R-Tree myisam支持 Full-text myisam支持(全文索引) .Memory引擎下只有“=”条件才会使用索引 ...

  5. Oracle RAC集群安装之:Grid软件安装过程蓝屏

    前几天在安装一套RAC服务器的过程中,遇到了蓝屏事件,折腾了蛮久(在排查是否存在硬件问题上花费了相当多一部分时间),整个过程大概说明如下: 1.两台华为的PC SERVER,操作系统为WIN SERV ...

  6. 2016 系统设计第一期 (档案一)MVC 引用 js css

    @Styles.Render("~/Bootstrap/css/bootstrap-theme.css") @Scripts.Render("~/jQuery/jquer ...

  7. Hadoop1.0.3集成eclipse开发

    本文来自:http://www.ilablog.org/%E7%BC%96%E8%AF%91hadoop-eclipse%E6%8F%92%E4%BB%B6/ 本人由于工作原因目前没有亲自尝试,那位尝 ...

  8. hadoop Safe mode is ON 的解决办法

    hadoop Safe mode is ON 的解决办法 搭了一个hadoop集群环境,近期总是出现读写文件错误的情况,查看name node的日志显示 (Safe mode is ON) Safe ...

  9. 安装ADT Cannot complete the install because one or more required items could not be found.

    点击进行安装,将会弹出 错误提示是: Cannot complete the install because one or more required items could not be found ...

  10. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...