Node.js的是建立在Chrome的JavaScript的运行时,可方便地构建快速,可扩展的网络应用程序的平台。

Node.js使用事件驱动、非阻塞I/ O模型,是轻量级、高效、完美的跨分布式设备运行数据密集型实时应用。
 
1、安装Node.js:http://www.nodejs.org/
      测试安装正确性:安装之后会有一个命令行工具。打开命令行工具,然后输入node helloworld.js。
helloworld.js中只需简单的一句话:console.log("Hello World!"); 
2、构建应用的模块——一个基础的HTTP服务器(server.js)
var http = require("http");
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服务器模块并启动。

var
server = require("./server");
server.start();
   
  测试:在命令行输入“node
index.js”,然后打开浏览器访问:http://localhost:8888/ ,网页显示“Hello
World!”。
 
   
这样我们就可以定义不同的模块分别放在不同的文件里了。
3、构建路由选择模块(router.js)
4、加入请求处理程序模块(requestHandlers.js)
 
下面以上传文件为例,展现各个模块间的关系。
 
server.js
var http
= 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;

router.js
function
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;

 
requestHandler.js
var querystring =
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;

 
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;
handle[/show"] =
requestHandlers.show;

server.start(router.route, handle);

 

初涉Node.js的更多相关文章

  1. asp.net程序员初涉node.js

    之前一直听说node.js在处理网站大规模并发上十分有用,所以有一定规模的公司都在使用node.我在工作中只用过jquery,属于那种边做功能边学习的那一种.甚至连原生的js都不太会写,只是知道语法差 ...

  2. 初涉node.js做微信测试公众号一路填坑顺便发现个有趣的其他漏洞

    [微信测试公众号] 半年前耍着玩搭起来的“微信简历”,是LAMP版的,很皮毛. 微信的官方文档在这 http://mp.weixin.qq.com/wiki/index.php 1.获取access ...

  3. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  4. 利用Node.js的Net模块实现一个命令行多人聊天室

    1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...

  5. Node.js:进程、子进程与cluster多核处理模块

    1.process对象 process对象就是处理与进程相关信息的全局对象,不需要require引用,且是EventEmitter的实例. 获取进程信息 process对象提供了很多的API来获取当前 ...

  6. Node.js:理解stream

    Stream在node.js中是一个抽象的接口,基于EventEmitter,也是一种Buffer的高级封装,用来处理流数据.流模块便是提供各种API让我们可以很简单的使用Stream. 流分为四种类 ...

  7. Node.js:Buffer浅谈

    Javascript在客户端对于unicode编码的数据操作支持非常友好,但是对二进制数据的处理就不尽人意.Node.js为了能够处理二进制数据或非unicode编码的数据,便设计了Buffer类,该 ...

  8. node.js学习(二)--Node.js控制台(REPL)&&Node.js的基础和语法

    1.1.2 Node.js控制台(REPL) Node.js也有自己的虚拟的运行环境:REPL. 我们可以使用它来执行任何的Node.js或者javascript代码.还可以引入模块和使用文件系统. ...

  9. Node.js npm 详解

    一.npm简介 安装npm请阅读我之前的文章Hello Node中npm安装那一部分,不过只介绍了linux平台,如果是其它平台,有前辈写了更加详细的介绍. npm的全称:Node Package M ...

随机推荐

  1. C#读取shp的属性信息

    一个完整的ESRI的shape文件包括一个主文件,一个索引文件,和一个dBASE表文件.主文件是一个直接存取,变记录长度文件,其中每个记录描述一 个由其顶点列表组成的shape.在索引文件中,每条记录 ...

  2. weimi 短信API post方式的简易代码。

    http://www.weimi.cc/example-csharp.html string mobile = "<enter your mobiles>", con ...

  3. EasyPusher推流服务接口的.NET导出

    本文是在使用由 EasyDarwin 团队开发的EasyPusher时导出的C++接口的.NET实现 public class EasyPushSDK { public EasyPushSDK() { ...

  4. 使用Bootstrap-Table 遇到的问题

    最近用bootstrap-Table插件,遇到的问题记录下: 1.如何按条件查询时,pageNumber重置为1? $('#btnSub').bind('click', function () { $ ...

  5. 64位Windows7升级IE11后无法启动的解决办法

    1.控制面板\网络和 Internet\Internet选项 2.在高级选项卡的“安全”组配置

  6. php 图片调整大小 封装类【转载】

    <?php class ImageResize { private $image; private $img_des; private $image_type; private $permiss ...

  7. 建立临时的表 数据 空值 与 NULL 转换

    if exists ( select * from INFORMATION_SCHEMA.tables where table_name = 'Config_Order_ChargesToPreVie ...

  8. 第十篇、让UIScrollView的滚动条常显

    UIScrollView滚动条一直显示 1.我们知道滚动条是一个UIImageView, 滚动条隐藏是因为设置了alpha属性为0, 所有我们写一个UIImageView的分类 #define noD ...

  9. HTML+CSS学习笔记 (14) - 单位和值

    标签:HTML+CSS 颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命 ...

  10. 【Unity3D】Unity3D之 Resources.Load 动态加载资源

    [Unity3D]Unity3D之 Resources.Load 动态加载资源 1.Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹 ...