nodejs -- fs模块 ---> readFile 函数 1) fs.readFile(filename, "binary", function(error, file) 2) response.write(file, "binary");
一:代码:
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 -- fs模块 ---> readFile 函数 1) fs.readFile(filename, "binary", function(error, file) 2) response.write(file, "binary");的更多相关文章
- nodejs模块——fs模块
fs模块用于对系统文件及目录进行读写操作. 一.同步和异步 使用require('fs')载入fs模块,模块中所有方法都有同步和异步两种形式. 异步方法中回调函数的第一个参数总是留给异常参数(exce ...
- node.js http模块和fs模块上机实验·
httpserver const httpserver = require('http'); var server = httpserver.createServer(function (req,re ...
- nodejs读取文件时相对路径的正确写法(使用fs模块)
在开发nodejs中,我们往往需要读取文件或者写入文件,最常用的模块就是fs核心模块.一个最简单的写入文件的代码如下(暂时不考虑回调函数): fs.readFile("./test.txt& ...
- [Nodejs] node的fs模块
fs 模块 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作 API. Node 导入文件系统模块(fs).Node.js 文件系统(fs 模块)模块中的方法均有异步和同步版本,例如 ...
- nodejs学习笔记一( sublime、atom开发环境,http模块,fs模块的初识)
http服务 let server = http.createServer(function(req,res){ }); 监听: server.listen(8080); re ...
- nodejs之fs模块
nodejs中的file system文件系统模块 1.文件的读取readFile //引入文件系统模块 const fs = require('fs'); //文件读取是异步操作 fs.readFi ...
- nodejs入门API之fs模块
fs模块下的类与FS常量 fs模块下的主要方法 fs的Promise API与FileHandle类 一.fs模块下的类 1.1 fs.Dir:表示目录流的类,由 fs.opendir().fs.op ...
- nodejs之fs 模块
1.fs模块函数 * .fs.stat 检测是文件还是目录 * .fs.mkdir 创建目录 * .fs.writeFile 创建写入文件 * .fs.appendFile 追加文件 * .fs.re ...
- nodejs中的fs模块中的方法
nodejs中的fs模块 引入模块 const fs =require("fs") 检测文件是否存在fs.stat(path,callback) fs.stat("./n ...
随机推荐
- Docker Kubernetes 常用命令
Docker Kubernetes 常用命令 增 # 通过文件名或标准输入创建资源. kubectl create # 读取指定文件内容,进行创建.(配置文件可指定json,yaml文件). kube ...
- shell批量修改mysql用户密码
需求 现在有这么一个需求, 需要大批量修改用户的密码, 需要注意的规则是: 必须添加的字符: *$#sjdKw% 用户名的第一位+*$#sjdKw%+用户名的最后一位,比如用户名chenglee,密码 ...
- Ubuntu 安装 Docker CE
注:本文转载自<Docker入门> 警告:切勿在没有配置 Docker APT 源的情况下直接使用 apt 命令安装 Docker. 准备工作 系统要求 Docker CE 支持以下版本的 ...
- Pandas 基础(12) - Stack 和 Unstack
这节的主题是 stack 和 unstack, 我目前还不知道专业领域是怎么翻译的, 我自己理解的意思就是"组成堆"和"解除堆". 其实, 也是对数据格式的一种 ...
- _quest_mod
该功能实现对任务的优化,设定接受任务的条件,比如VIP等级几或者军衔多少持有何种物品才可以接受任务,同时可以配置任务的随机奖励及几率,以上修改都会在任务文本中体现.还支持任务传送功能,接完任务后,可和 ...
- Listview自定义了子View导致listview的onitemclick事件无效
原因是子View的点击事件抢占了listview的点击事件 解决办法: 1. 子View根布局 设置 android:descendantFocusability="blocksDescen ...
- 算法笔记--极大极小搜索及alpha-beta剪枝
参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...
- CentOS 7安装后的配置
一.设置IP地址.网关DNS 说明:CentOS 7.x默认安装好之后是没有自动开启网络连接的,所 以需要我们自己配置. 在命令行输入#vi /etc/sysconfig/network-scrip ...
- C#时间戳的简单实现
Introduction: 在项目开发中,我们都经常会用到时间戳来进行时间的存储和传递,最常用的Unix时间戳(TimeStamp)是指格林尼治时间1970年1月1日0时(北京时间1970年1月1日8 ...
- C#反射详解
http://blog.csdn.net/educast/article/details/2894892(转) 两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏 ...