从零开始学习Node.js系列教程二:文本提交与显示方法
作者:MIN飞翔 发布时间:2024-05-08 09:35:30
标签:Node.js,文本
本文实例讲述了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:
知识点:
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程序设计有所帮助。


猜你喜欢
- 本文实例讲述了Python 26进制计算方法。分享给大家供大家参考。具体分析如下:题目是这样的:假设A=1,B=2,C=3...AA=27,
- 一、安装selenium库问题1:什么是selenium模块?基于浏览器自动化的一个模块。 问题2:selenium模块有什么作用
- 比如在学习list、tuple、dict、str、os、sys等模组的时候,利用Python的自带文档可以很快速的全面的学到那些处理的函数。
- 最近在做一个项目,用双通道神经网络,每个通道输入不同数据训练,具有相同label。开始没想到如何实现,网上很多例子都是单通道,即便找到双通道
- 废话不多说了,直接给大家贴代码了,具体代码如下所示:<!DOCTYPE html><html lang="en&
- ObjectUtil组件其实就是单例模式的最好范例,声明调用各个组件的时候,用ObjectUtil调用,可以有效的防止调用过多的类而导致错误
- 根据代码中运行的结果来看,主要由以下几种:1. sum():将array中每个元素相加的结果2. axis对应的是维度的相加。比如:1、ax
- Python3将数据保存为txt文件的方法,具体内容如下所示:f = open("data/model_Weight.txt&qu
- 一、题目描述A:先输出提示语句,并接受用户输入的年、月。B:根据用户输入的年,先判断是否是闰年。C:根据用户输入的月来判断月的天数。D:用循
- PHP 5.0.0 和PHP 4.0.38 于2004年7月13日同时发布,这是一个值得我们PHP爱好者的一大喜讯。期盼已久的PHP5终于出
- 看过数据库的备份与还原。大多数都是用组件来完成的。其实可通过sql语句来完成。 由于时间关系,未对参数进行验证和界面美化。代码
- 本文列举了兼容 IE 和 FF 的换行 CSS 推荐样式,详细介绍了word-wrap同word-break的区别。兼容 IE 和 FF 的
- 前言本篇文章主要讲解vue响应式原理的逻辑,也就是vue怎么从最开始一步步推导出响应式的结构框架。 先从头构建一个简单函数推导出Vue3的R
- vue项目依赖升级报错处理1.Vue Router 升级到3.5.1报错:Navigation cancelled from "/
- 说明1、导入unittest模块。2、导入被测对象。3、创建测试类unittest.TestCase。4、重写setUp和tearDown(
- 一直在学习系统托盘的实现,于是自己写了一个简单的系统托盘实例,右键包括演示、最大化、最小化、退出和关于。在python2.6下测试通过。注意
- 一 概念1. 原理2. 好处不同项目可能用到的环境不同,运用虚拟环境能将不同环境分隔开二 virtualenvvirtual 虚拟的1. 安
- 本文实例讲述了Python3.5常见内置方法参数用法。分享给大家供大家参考,具体如下:Python的内置方法参数详解网站为:https://
- 本文介绍了通过Cursor 工具使用GPT-4的方法。Cursor 是集成了 GPT-4 的 IDE 工具,目前免费并且无需 API Key
- python 字符串替换 是python 操作字符串的时候经常会碰到的问题,这里简单介绍下字符串替换方法。python 字符串替换可以用2种