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) 启动服务器:

nodejs -- fs模块 ---> readFile 函数         1) fs.readFile(filename, "binary", function(error, file)       2) response.write(file, "binary");

3) http访问:

nodejs -- fs模块 ---> readFile 函数         1) fs.readFile(filename, "binary", function(error, file)       2) response.write(file, "binary");

1.6 参考链接:

  nodejs 入门

上一篇:细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型


下一篇:go exec: "gcc": executable file not found in %PATH%