Hi;
I'm a novice learning node.js, and I'm working my way through an
e-book that I downloaded:
http://www.nodebeginner.org/
The last example in the beginner's book does not execute properly.
When the name of the file is submitted, the HTML is sent to the
browser, but it is not executed, and shows up as text.
response.writeHead(200, { "Content-Type": "text/plain" });
response.write(" ... received image:<br/>");
response.write("<img src='/show' />");
response.end();
Can someone please tell me what it is that I am doing wrong?
============================================================
// Index_01.js
var njs_server = require("./server_01");
var njs_router = require("./Router_01");
var njs_request_handler = require("./Request_Handlers_01");
var req_handle_set = {};
req_handle_set["/"] = njs_request_handler.Request_start;
req_handle_set["/start"] = njs_request_handler.Request_start;
req_handle_set["/upload"] = njs_request_handler.Request_upload;
req_handle_set["/show"] = njs_request_handler.Request_show;
njs_server.main_start(njs_router.route_path, req_handle_set);
//# sourceMappingURL=Index_01.js.map
============================================================
// Router_01.js
function def_route_path(p2_req_handle_set, p_pathname, p_request,
p_response, postData) {
console.log("About to route a request for " + p_pathname);
if (typeof p2_req_handle_set[p_pathname] === 'function') {
p2_req_handle_set[p_pathname](p_request, p_response, postData);
}
else {
console.log("No request handler found for " + p_pathname);
p_response.writeHead(404, { "Content-Type": "text/plain" });
p_response.write("404 Not found");
p_response.end();
}
}
exports.route_path = def_route_path;
//# sourceMappingURL=Router_01.js.map
============================================================
// Server_01.js
var http = require("http");
var url = require("url");
var cnt1 = 1;
var cnt2 = 1;
function def_main_start(p_vfn_route_path, p_req_handle_set) {
var body1 = '<html>' +
'<head>' +
'<meta htp-equiv="Content-Type" content="text/html; charset=TF-8"
/>' +
'</head>' +
'<body>';
var body2 = '</body>' + '</html>';
var L_port = process.env.port || 1337;
function onRequest_03(request, response) {
var postData = "";
var pathname = url.parse(request.url).pathname;
var req_recv = "<br/>Request for " + pathname + " received." +
cnt1++;
p_vfn_route_path(p_req_handle_set, pathname, request, response,
postData);
}
http.createServer(onRequest_03).listen(L_port);
console.log("Server has started.");
}
exports.main_start = def_main_start;
//# sourceMappingURL=server_01.js.map
============================================================
// Request_Handlers_01.js
var m_exec = require("child_process").exec;
var m_query_string = require("querystring");
var m_fs = require("fs.extra");
var m_formidable = require("formidable");
var m_cntr = 1;
var exec_handler = (function () {
function exec_handler(p2_response) {
this.v_response = p2_response;
}
exec_handler.prototype.exec_01 = function (error, stdout, stderr) {
this.v_response.writeHead(200, { "Content-Type": "text/plain" });
this.v_response.write(stdout);
this.v_response.write(" --- cntr : " + m_cntr++);
this.v_response.end();
};
return exec_handler;
}());
var m_body_02 = '<html>' +
'<head>' +
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'
+
'</head>' +
'<body>' +
'<form action="/upload" enctype="multipart/form-data" method="post">' +
"<br/>" +
'<input type="file" name="upload" multiple="multiple">' + "<br/>" +
'<input type="submit" value="Upload file" />' + "<br/>" + "<br/>" +
'</form>' +
'</body>' +
'</html>';
function Request__start(p4_request, p4_response, p4_postData) {
console.log("Request handler 'start' was called.");
p4_response.writeHead(200, { "Content-Type": "text/html" });
p4_response.write(m_body_02);
p4_response.end();
}
var m_pic_source_02 = "Pics_02\\p100_5447.JPG";
function Request__upload_02(p7_request, p7_response, p7_postData) {
console.log(" >>> Request handler 'Request__upload_02' was called.");
var L_form = new m_formidable.IncomingForm();
p7_response.vfn_show__rspn_image = show__rspn_image;
L_form.parse(p7_request, function (error, fields, files) {
console.log("parsing done : " + files.upload.path);
/* This is executing on a drive (partition) other than C: - copy
the file
*/
m_fs.copy(files.upload.path, m_pic_source_02, function (error) {
if (error) {
console.log("error : " + files.upload.path + " ... " +
error);
m_fs.unlink(m_pic_source_02, function (error) {
if (error) {
console.log("error unlink : " + m_pic_source_02 + "
... " + error);
}
else
m_fs.copy(files.upload.path, m_pic_source_02,
function (error) {
if (error) {
console.log("error 2 : " +
files.upload.path + " ... " + error);
}
else
show__image(p7_response);
});
});
}
else
show__image(p7_response);
});
});
}
// This executes, but is rendered as text within the browser,
// and the HTML is not executed.
function show__image(p8_response) {
p8_response.writeHead(200, { "Content-Type": "text/plain" });
p8_response.write(" ... received image:<br/>");
p8_response.write("<img src='/show' />");
p8_response.end();
}
function show__rspn_image() {
this.writeHead(200, { "Content-Type": "text/plain" });
this.write(" ... response received image:<br/>");
this.write("<img src='/show' />");
this.end();
}
function Request__show(p_request, p_response, postData) {
console.log("Request handler 'show' was called.");
p_response.writeHead(200, { "Content-Type": "text/plain" });
m_fs.createReadStream(m_pic_source_02).pipe(p_response);
}
exports.Request_start = Request__start;
exports.Request_upload = Request__upload_02;
exports.Request_show = Request__show;
============================================================
============================================================
Thanks,
Wally
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/4157a621-2df2-42ff-aace-01cf2db29fa9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.