初涉Node.js
function start() {
http.createServer(function
(request, response) {
response.writeHead(200, { "Content-Type": "text/plain" });
response.write("Hello
World!");
response.end();
}).listen(8888);
}
exports.start =
start;
在主要脚本(index.js)里引用http服务器模块并启动。
server = require("./server");
server.start();
测试:在命令行输入“node
index.js”,然后打开浏览器访问:http://localhost:8888/ ,网页显示“Hello
World!”。
这样我们就可以定义不同的模块分别放在不同的文件里了。
= require("http");
var url = require("url");
function start(route, handle)
{
function onRequest(request, response) {
var pathname =
url.parse(request.url).pathname;
console.log("Request
for " + pathname + " received.");
route(handle, pathname, response,
request);
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
route(handle, pathname, response, request) {
console.log("About
to route a request for " + pathname);
if (typeof
handle[pathname] === 'function')
{
handle[pathname](response,
request);
}
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;
require("querystring");
var fs = require("fs");
var formidable =
require("formidable");
var sys = require("sys");
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" enctype="multipart/form-data" ' +
'method="post">'
+
'
< input
type="file" name="upload" multiple="multiple" />' +
'
< input
type="submit" value="Upload file" />' +
'
< /form>'
+
'
< /body>'
+
'
< /html>';
response.writeHead(200,
{ "Content-Type": "text/html" });
response.write(body);
response.end();
}
function upload(response, request)
{
console.log("Request
handler 'upload' was called.");
var
form = new
formidable.IncomingForm();
form.uploadDir
= "temp";
console.log("about
to parse");
form.parse(request,
function (error, fields, files)
{
console.log("parsing
done");
//sys.puts(sys.inspect(files.upload,
true, null));
fs.renameSync(files.upload.path,
"./temp/test.jpg");
response.writeHead(200,
{ "Content-Type": "text/html" });
response.write("received
image: < br/>");
response.write("
< img
src='/show' />");
response.end();
});
}
function show(response) {
console.log("Request
handler 'show' was called.");
fs.readFile("./temp/test.jpg",
"binary", function (error, file) {
if
(error) {
response.writeHead(500,
{ "Content-Type": "text/plain" });
response.write(error
+ "\n");
response.end();
}
else {
response.writeHead(200,
{ "Content-Type": "image/jpeg" });
response.write(file,
"binary");
response.end();
}
});
}
exports.start = start;
exports.upload = upload;
exports.show = show;
var router = require("./router");
var requestHandlers =
require("./requestHandlers");
var handle = {};
handle[/"] =
requestHandlers.start;
handle[/start"] =
requestHandlers.start;
handle[/upload"] =
requestHandlers.upload;
handle[/show"] =
requestHandlers.show;
server.start(router.route, handle);
初涉Node.js的更多相关文章
- asp.net程序员初涉node.js
之前一直听说node.js在处理网站大规模并发上十分有用,所以有一定规模的公司都在使用node.我在工作中只用过jquery,属于那种边做功能边学习的那一种.甚至连原生的js都不太会写,只是知道语法差 ...
- 初涉node.js做微信测试公众号一路填坑顺便发现个有趣的其他漏洞
[微信测试公众号] 半年前耍着玩搭起来的“微信简历”,是LAMP版的,很皮毛. 微信的官方文档在这 http://mp.weixin.qq.com/wiki/index.php 1.获取access ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 利用Node.js的Net模块实现一个命令行多人聊天室
1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...
- Node.js:进程、子进程与cluster多核处理模块
1.process对象 process对象就是处理与进程相关信息的全局对象,不需要require引用,且是EventEmitter的实例. 获取进程信息 process对象提供了很多的API来获取当前 ...
- Node.js:理解stream
Stream在node.js中是一个抽象的接口,基于EventEmitter,也是一种Buffer的高级封装,用来处理流数据.流模块便是提供各种API让我们可以很简单的使用Stream. 流分为四种类 ...
- Node.js:Buffer浅谈
Javascript在客户端对于unicode编码的数据操作支持非常友好,但是对二进制数据的处理就不尽人意.Node.js为了能够处理二进制数据或非unicode编码的数据,便设计了Buffer类,该 ...
- node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法
1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...
- Node.js npm 详解
一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...
随机推荐
- javascript事件代理(委托)
之前有接触过事件代理,但是印象并不深刻.这次记下来加强印象. 用个大家比较常见的代码举例子: html dom结构: <ul id="ul1"> <li>0 ...
- MongoDB - Introduction of the mongo Shell
Introduction The mongo shell is an interactive JavaScript interface to MongoDB. You can use the mong ...
- mvc路由规则相关
1,可以创建多条路由规则,每条路由规则的name属性不能相同 2,路由规则是有顺序的,如果被前面的规则匹配了,那么后面的规则就没有机会了 下面是一条路由规则的代码 routes.MapRoute( n ...
- UML类图几种关系的总结[转]
原文地址:http://www.open-open.com/lib/view/open1328059700311.html 在UML类图中,常见的有以下几种关系: 泛化(Generalization) ...
- Virtualizing WrapPanel VS toolkit:WrapPanel
用toolkit:WrapPanel的时候,LIST太大,内存不行,等下我试试 Virtualizing WrapPanel这个 http://www.codeproject.com/Articles ...
- 把url后面的.html去掉
$url = "http://haichuang1997.bmlink.com/supply/dc_355472.html";echo rtrim($url, '.html');
- 当年的笔记_apache配置虚拟主机
下午需要,在网上找了一堆,没找到合适的,翻出来自己当年的笔记,还是自己记的容易理解. 解决方案1:通过端口来区分 1>添加一个虚拟主机1.在d盘下新建www目录,如:d:/www. 2.修改ht ...
- QQ好友列表向左滑动出现置顶、删除--第三方开源--SwipeMenuListView
SwipeMenuListView是在github上的第三方开源项目,该项目在github上的链接地址是:https://github.com/baoyongzhang/SwipeMenuListVi ...
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
C#中test测试地 方法一. 1.从NUnit官网(http://www.nunit.org/index.php)下载最新版本NUnit,当前版本为NUnit2.5.8. 2.安装后,在VS2008 ...
- hexo-github 博客搭建
安装nodejs 从官网下载系统对应的源码 wget -qO- https://raw.githubusercontent.com/creationix/nvm/master/install.sh | ...