一:代码:

1.1 入口文件: 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);

1.2 服务器文件: server.js

 var http = require("http");
var url = require("url"); function start(route, handle){
http.createServer(function(request, response) { // console.log(url.parse(request.url));
// console.log(url.parse(request.url).query);
var pathname = url.parse(request.url).pathname;
console.log('request for [' +pathname+ "] received."); //接收请求数据:
var postData = "";
request.setEncoding("utf8"); request.on("data", function(chunk){
postData += chunk;
console.log("Received POST data chunk '" +chunk+"'");
}); request.on("end", function(){
route(handle, pathname, response, postData);
}); }).listen(8888); console.log("server has started.");
} exports.start = start;

1.3 路由文件: 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;

1.4 请求处理程序文件: requestHandles.js

 var querystring = require("querystring");
var fs = require("fs"); 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">'+
'<input type="text" value="" name="userName"/>'+
'<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 allData: " +querystring.parse(postData)+ "\n");
response.write("You've sent userName: " +querystring.parse(postData).userName+ "\n");
response.write("You've sent text: " +querystring.parse(postData).text+ "\n");
response.end();
} function show(response, postData){
console.log("Request handle 'show' was called.");
39 var filename = "C:/Users/dc5yy/Desktop/图片_1/06.jpg";
40 fs.readFile(filename, "binary", function(error, file){
if (error) {
42 response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
response.end();
} else{
46 response.writeHead(200, {"Content-Type": "image/jpg"});
47 response.write(file, "binary");
response.end();
}
});
} exports.start = start;
exports.upload = upload;
exports.show = show;

1.5 结果:

1)要读取的图片 地址:

var filename = "C:/Users/dc5yy/Desktop/图片_1/06.jpg";

2) 启动服务器:

3) http访问:

1.6 参考链接:

  nodejs 入门

nodejs -- fs模块 ---> readFile 函数 1) fs.readFile(filename, "binary", function(error, file) 2) response.write(file, "binary");的更多相关文章

  1. nodejs模块——fs模块

    fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...

  2. node.js http模块和fs模块上机实验·

    httpserver const httpserver = require('http'); var server = httpserver.createServer(function (req,re ...

  3. nodejs读取文件时相对路径的正确写法(使用fs模块)

    在开发nodejs中,我们往往需要读取文件或者写入文件,最常用的模块就是fs核心模块.一个最简单的写入文件的代码如下(暂时不考虑回调函数): fs.readFile("./test.txt& ...

  4. [Nodejs] node的fs模块

    fs 模块 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作 API. Node 导入文件系统模块(fs).Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如 ...

  5. nodejs学习笔记一( sublime、atom开发环境,http模块,fs模块的初识)

    http服务   let server = http.createServer(function(req,res){       });   监听: server.listen(8080);   re ...

  6. nodejs之fs模块

    nodejs中的file system文件系统模块 1.文件的读取readFile //引入文件系统模块 const fs = require('fs'); //文件读取是异步操作 fs.readFi ...

  7. nodejs入门API之fs模块

    fs模块下的类与FS常量 fs模块下的主要方法 fs的Promise API与FileHandle类 一.fs模块下的类 1.1 fs.Dir:表示目录流的类,由 fs.opendir().fs.op ...

  8. nodejs之fs 模块

    1.fs模块函数 * .fs.stat 检测是文件还是目录 * .fs.mkdir 创建目录 * .fs.writeFile 创建写入文件 * .fs.appendFile 追加文件 * .fs.re ...

  9. nodejs中的fs模块中的方法

    nodejs中的fs模块 引入模块 const fs =require("fs") 检测文件是否存在fs.stat(path,callback) fs.stat("./n ...

随机推荐

  1. Python 语言来编码和解码 JSON 对象

    Json函数: json.dumps: Python标准库中的json模块,集成了将数据序列化处理的功能. 将 Python 对象编码成 JSON 字符串 语法: json.dumps(obj, sk ...

  2. 用PHP实现反向代理服务器

    什么是反向代理: 百度百科有云: 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给int ...

  3. Poj3984 迷宫问题 (BFS + 路径还原)

    Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...

  4. QT创建TCP Socket通信

    最近在学习QT,了解到QT可以进行SOCKET网络通信,进行学习,并建立一个简单的聊天DEMO.为了测试是否能与VS2012下的程序进行通信,在VS2012下建立一个客户端程序,进行通信测试,发现可以 ...

  5. linux bash tutorial

    bash read-special-keys-in-bash xdotool linux 登录启动顺序

  6. Build Tool

    building tool: 一.building tools 为什么主流? Gradle 是目前比较流行的构建工具之一,Android Studio 中集成的就是 Gradle,并针对 Androi ...

  7. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

  8. js-input框中写入的小写小写字母全部转换成大写字母的js代码

    <input type="text" id="blinitials" name="blinitials"  onkeyup=" ...

  9. Linux查看服务器硬件配置命令

    一.查看服务器硬件信息 dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product|Seria ...

  10. Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom

    time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...