从零开始学习Node.js系列教程二:文本提交与显示方法


Posted in Javascript onApril 13, 2017

本文实例讲述了Node.js文本提交与显示方法。分享给大家供大家参考,具体如下:

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 + "'.");
  });
  request.addListener("end", function() {
   console.log("data received ending" + pathname);
   route(handle, pathname, response, postData);
  });
 }
 http.createServer(onRequest).listen(8888);
 console.log("Server has started.");
}
exports.start = start;

requestHandlers.js

var querystring = require("querystring");
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 the text: "+
 querystring.parse(postData).text);
 response.end();
}
exports.start = start;
exports.upload = upload;

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;

result:

从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法从零开始学习Node.js系列教程二:文本提交与显示方法

知识点:

require和exports的用法:

index.js中代码

var Hello = require('.hello');
hello = new Hello();
hello.setName('Joey');
hello.sayHello();

hello.js中代码

function Hello(){
  var name;
  this.setName = function(thyName){
    name = thyName;
  }
  this.sayHello = function(){
    console.log('Hello ' + name);
  }
}
//exports.Hello = Hello; //此时我们在其他文件中需要通过 require('./hello').Hello来获取Hello对象,这种写法有点冗余
module.exports = Hello; //输出的就是Hello对象本身,不是上面的exports,上面的是暴露.Hello,.Hello赋予了Hello对象

希望本文所述对大家nodejs程序设计有所帮助。

Javascript 相关文章推荐
非常有用的40款jQuery 插件推荐(系列二)
Dec 25 Javascript
jquery实现奇偶行赋值不同css值
Feb 17 Javascript
jQuery动画animate方法使用介绍
May 06 Javascript
jquery默认校验规则整理
Mar 24 Javascript
jquery数组封装使用方法分享(jquery数组遍历)
Mar 25 Javascript
jquery使用animate方法实现控制元素移动
Mar 27 Javascript
javascript中call和apply的用法示例分析
Apr 02 Javascript
javascript中scrollTop详解
Apr 13 Javascript
jQuery实现将div中滚动条滚动到指定位置的方法
Aug 10 Javascript
AngularJS 自定义过滤器详解及实例代码
Sep 14 Javascript
js实现模糊匹配功能
Feb 15 Javascript
vue使用v-model进行跨组件绑定的基本实现方法
Apr 28 Vue.js
从零开始学习Node.js系列教程一:http get和post用法分析
Apr 13 #Javascript
基于Vue实现tab栏切换内容不断实时刷新数据功能
Apr 13 #Javascript
JavaScript数据结构之二叉树的计数算法示例
Apr 13 #Javascript
JavaScript数据结构之二叉树的删除算法示例
Apr 13 #Javascript
JavaScript数据结构之二叉树的查找算法示例
Apr 13 #Javascript
jQuery EasyUI 为Combo,Combobox添加清除值功能的实例
Apr 13 #jQuery
JavaScript中this的用法及this在不同应用场景的作用解析
Apr 13 #Javascript
You might like
php smarty 二级分类代码和模版循环例子
2011/06/16 PHP
php session劫持和防范的方法
2013/11/12 PHP
php-redis中的sort排序函数总结
2015/07/08 PHP
用javascript将数据库中的TEXT类型数据动态赋值到TEXTAREA中
2007/04/20 Javascript
jQuery 开发者应该注意的9个错误
2012/05/03 Javascript
js获取html参数及向swf传递参数应用介绍
2013/02/18 Javascript
细说javascript函数从函数的构成开始
2013/08/29 Javascript
通过JQuery将DIV的滚动条滚动到指定的位置方便自动定位
2014/05/05 Javascript
js实现表单检测及表单提示的方法
2015/08/14 Javascript
jquery获取url参数及url加参数的方法
2015/10/26 Javascript
jquery背景跟随鼠标滑动导航
2015/11/20 Javascript
Jquery与Bootstrap实现后台管理页面增删改查功能示例
2017/01/22 Javascript
Ionic2开发环境搭建教程
2020/08/20 Javascript
gulp安装以及打包合并的方法教程
2017/11/19 Javascript
解决低版本的浏览器不支持es6的import问题
2018/03/09 Javascript
angular2 组件之间通过service互相传递的实例
2018/09/30 Javascript
为什么要使用Vuex的介绍
2019/01/19 Javascript
小程序跳转到的H5页面再跳转回跳小程序的方法
2020/03/06 Javascript
JS JQuery获取data-*属性值方法解析
2020/09/01 jQuery
[36:45]TNC vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python3设计模式之简单工厂模式
2017/10/17 Python
python读取视频流提取视频帧的两种方法
2020/10/22 Python
使用python将大量数据导出到Excel中的小技巧分享
2018/06/14 Python
Python发送手机动态验证码代码实例
2020/02/28 Python
详细分析Python可变对象和不可变对象
2020/07/09 Python
通过实例简单了解Python sys.argv[]使用方法
2020/08/04 Python
瑞贝卡·泰勒官方网站:Rebecca Taylor
2016/09/24 全球购物
美国最大的农村生活方式零售店:Tractor Supply Company(TSC)
2017/05/15 全球购物
莫斯科绝对前卫最秘密的商店:SVMoscow
2017/10/23 全球购物
SIXPAD智能健身仪英国官网:革命性的训练装备品牌
2018/09/27 全球购物
澳大利亚领先的亚麻品牌:Bed Threads
2019/12/16 全球购物
美国宠物护理专家:Revival Animal Health
2020/01/05 全球购物
环境工程与管理大学毕业生求职信
2013/10/02 职场文书
民事起诉书范本
2015/05/19 职场文书
Django中session进行权限管理的使用
2021/07/09 Python
Mysql实现简易版搜索引擎的示例代码
2021/08/30 MySQL