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. Flashback删除(闪回删除)

    oracle 9i以前,当drop一个表时,到该表的所有引用都会从数据字典中删除. oracle 10g及以后,当drop一个表时,数据库根本没有删除表,而只是重命名了并放入了回收站.即当发出drop ...

  2. Keil V4.72升级到V5.1X之后

    问题描述 Keil V4.72升级到V5.1x之后,原来编译通过的工程,出现了如下错误: .\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\STM32f ...

  3. WinForm 换行问题 textbox (转)

    WinForm 换行问题 textbox 今天碰到一段string在label中能正常换行,但是在textbox中却无法换行的问题. 首先考虑是换行符的问题.在网上查了些资料: 1.TextBox 中 ...

  4. Telerik_2012_Q3 RadGrid 汉化

    ChineseRadGridLocalizationProvider.cs using System; using System.Collections.Generic; using System.L ...

  5. 1038: [ZJOI2008]瞭望塔 - BZOJ

    Description 致力于建设全国示范和谐小村庄的H村村长dadzhi,决定在村中建立一个瞭望塔,以此加强村中的治安.我们将H村抽象为一维的轮廓.如下图所示 我们可以用一条山的上方轮廓折线(x1, ...

  6. Linux下使用GDB调试程序

    问题描述:          Linux下使用GDB调试程序 问题解决:          (1)生成调试文件 注:         使用命令   gdb IOStream.c   -o IOStre ...

  7. 通过SQL Server 2008 访问MySQL(转)

    在公司中经常会遇到部署多种数据库环境的情况,对于开发人员来说经常在不同数据库之间转换确实有些繁琐,本篇将介绍从SQL Server 操作MySQL 数据库的方法. 数据库测试环境 1. SQL Ser ...

  8. "Principles of Reactive Programming" 之<Actors are Distributed> (1)

    week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Clus ...

  9. hdu 4558 剑侠情缘

    思路:dp[i][j][k]表示在点(i,j)处能量的差值为k的方案数 转移的时候把差值取相反数就实现轮流了 代码如下: #include<iostream> #include<st ...

  10. hdu 3927 Math Geek

    纯数论题,不解释!!!! 代码如下: #include<stdio.h> int main(){ ,m; scanf("%d",&t); while(t--){ ...