初涉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 ...
随机推荐
- CSS其他
1.元素的宽度由内容撑开 display:inline;——不支持高度 display:inline-block;——在IE6下,不支持块标签 float position:absolute——每项设 ...
- java之旅——JDK版本下载
作为一名IT工作者,技术学无止境,最近开始学习java. 学习java就需要安装jdk,直接到官网上下载,总是找不到很好的版本,在资源中找到一个下载jdk的链接,想下载哪个版本都有. http://w ...
- Java注解(自定义注解、view注入)
注解这东西虽然在jdk1.5就加进来了,但他的存在还是因为使用Afinal框架的view注入才知道的.一直觉得注入特神奇,加了一句就可以把对应view生成了. 下面我们来认识一下注解这个东西 一.注解 ...
- WPF非UI线程获取修改控件属性值的方法
public class InvokeHelper { #region delegates private delegate object MethodInvoker(Control control, ...
- shell运行报 too many arguments错误
有时候shell在运行的时候可能会报 too many arguments错误,出现这种错误的一般情况是出现了多值问题,也就是一个变量可能有多个值了. 例:#!/bin/sh echo "I ...
- PHP实现前台同步显示后台任务进度
一次批量发送几千条短信. 如果直接在后台循环执行虽然可行,但是前台操作用户就只能坐着空等,完全看不到后台执行结果,所以考虑能不能有一种办法可以在php后台执行过程中同时在前台显示后台执行任务进度呢. ...
- js 倒计时 button不可用
function showtime() { fun_timedown(5); } function fun_timedown(time) { $("#timedown").val( ...
- 在浏览器中打开本地应用(iOS)
在浏览器中点击跳转到本地应用的方法(如果本地没有安装的话) 然后在浏览器中输入tianxiang://就能打开这个应用了 ................省略 遇到一个12年还是初中的小朋友,
- iOS 中对各种视图的截屏以及分享
1.一个第三方的工具,主要是对表视图.滚动视图.视图的扩展,用法也很简单 image = [tableview screenshot]; 2.然后将截的图片分享出去,在分享的时候,因为多个地方用到了截 ...
- floodfill算法解题示例
Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法.因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名.在GNU Go和扫雷中,Floo ...