Skip to content

Instantly share code, notes, and snippets.

@sapardi
Created February 28, 2012 03:35
Show Gist options
  • Save sapardi/1929207 to your computer and use it in GitHub Desktop.
Save sapardi/1929207 to your computer and use it in GitHub Desktop.
Read file in node.js
var http = require('http'),
fs = require('fs'),
step = require('step');
var file_path = __dirname + '/SCAN0004.JPG';
var file_size, file_content;
step(
function get_file_size() {
fs.stat(file_path, this);
},
function store_file_size(err, stat) {
file_size = stat.size;
this();
},
function read_file_into_memory() {
fs.readFile(file_path, this);
},
function createServer(err, file_content) {
http.createServer(function(request, response) {
response.writeHead(200, {
'Content-Type': 'image/jpeg',
'Content-Length': file_size
});
response.end(file_content);
}).listen(4000);
});
fs.stat(file_path, function(err, stat) {
var file_size = stat.size;
fs.readFile(file_path, function(err, file_content) {
if (err) throw err;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment