初涉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 ...
随机推荐
- YuXi-钰玺博客
本博客将与YuXi-钰玺博客进行同步更新! YuXi-钰玺博客:www.studenty.cn
- C#垃圾回收机制
C#属于托管的面相对象的语言,内存回收机制就是一个代表, C#有一套类似"全自动"的垃圾回收机制,也就是虚拟机会自动来判断执行内存的回收, 我们一般常用的Dispose(),Usi ...
- JavaScript之动画2
在JavaScript动画中,我们调用setInterval函数(setInterval动作的作用是在播放动画的时,每隔一定时间就调用函数,方法或对象),值得注意的是:setInterval它设置的时 ...
- 怎么解决tomcat占用8080端口问题图文教程(转)
亲测有效. 原因:可能是开了多个tomcat 原文网址:http://jingyan.baidu.com/article/1612d5006c3cdae20e1eee04.html 怎么解决tomc ...
- 《Cocos2d-x实战 工具卷》上线了
感谢大家一直以来的支持! 各大商店均开始销售:京东:http://item.jd.com/11659696.html当当:http://product.dangdang.com/23659809.ht ...
- 对ASP.Net的认识(三)
较为详细的整个请求处理流程 Application: BeginRequestApplication: PreAuthenticateRequestApplication: AuthenticateR ...
- 转帖:使用TortoiseGit处理代码冲突
原址:http://www.cnblogs.com/jason-beijing/p/5718190.html 场景一 user0 有新提交 user1 没有pull -> 写新代码 -&g ...
- band
#include<iostream> #include<math.h> using namespace std; int calculate(double amount[],i ...
- 关闭MyEclipse代码编辑器(breadcrumb)工具条
1. 在工具栏上找“Toggle Breadcrumb”按钮,单击使其恢复未选中状态即可 2. 如果找不到这个按钮.通过菜单“Window->Customize Perspective”打开对话 ...
- ant条件逻辑
<condition property="sdk-folder" value="E:\android\android-sdk\adt-bundle-windows- ...