Getting a binding error when trying to run Node http server - javascript

We are supposed to create a simple http node server that should respond to a root-url request with a file called index.html. Do not use ExpressJS. Code should have error checking and at least one callback. Put five or more html elements in your index.html. One of the elements should be a link to an external page.
This is the code I have:
var http = require("http");
var fs = require('fs');
var index = fs.readFileSync('index.html');
var server = http.createServer(function(request, response) {
fs.exists(index, function(exists) {
try {
if(exists) {
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write("<html>");
  response.write("<head>");
  response.write("<title>Hello World!</title>");
  response.write("</head>");
  response.write("<body>");
response.write("<div>");
  response.write("Hello World!");
response.write("</div>");
response.write("<a href='http://www.google.com' target='_blank'>Google</a>")
  response.write("</body>");
  response.write("</html>");
} else {
response.writeHead(500);
}
} finally {
response.end(index);
}
});
});
 
server.listen(80);
console.log("Server is listening");
And I am getting this binding error:
Server is listening
fs.js:166
binding.stat(pathModule._makeLong(path), cb);
^
TypeError: path must be a string
at Object.fs.exists (fs.js:166:11)
at Server.<anonymous> (/Users/rahulsharma/Desktop/server.js:8:4)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2112:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1970:22)
at TCP.onread (net.js:527:27)
Any thoughts?

Replacing the index variable with 'index.html' will do the job but
please Do NOT use fs.exists , read its API doc
http://nodejs.org/api/fs.html#fs_fs_exists_path_callback

Place the index.html with the .js file. Put all the html in that file.
var http = require("http");
var fs = require('fs');
var server = http.createServer(function(req, res) {
fs.readFile("index.html",function(err,content){
if(err){
throw err;
console.log("Error reading index file.");
res.send("Aw snap!");
}
else{
res.writeHead(200,{"Content-type":"text/HTML"});
res.end(content,"UTF-8");
}
});
});
server.listen(80);

According to you stack trace the error is inside this line:
fs.exists(index, function(exists)
What you pass to this function(which checks if given file exists) is actually content of the file. What you should pass as first argument is probably "index.html" instead of index variable

You are trying to call fs.exists which expects a string path and you are giving it a filehandler index.
That's why the error is:
path must be a string
Either try using the string "index.html" and do not read it sync there. Do it async in the exists callback
fs.exists("index.htm", function(){ fs.readFile("index.htm")

Related

Node returning error - "route is not defined"

I'm brand new to node and just running through a tutorial I found. I punched in the code as written, which as I understand ought to be importing the route function from the router file. However, the server encounters an error on trying to load the page returning the error in the title.
Here is my code:
// index.js; my main file I run through cmd
var server = require("./server");
var router = require("./router")
server.start(router.route);
// router.js; the route function is stored here
function route(pathname) {
console.log("About to rout request for " + pathname);
}
exports.route = route;
// server.js; the meat
var http = require("http");
var url = require("url");
function start() {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received");
route(pathname);
response.writeHead(200, { "Content-Type": "text/plain" });
response.write("Hello World!");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
While I'm asking, can anyone explain how the server.start(router.route) line is working? I thought that start function didn't take arguments.
While I'm asking, can anyone explain how the server.start(router.route) line is working? I thought that start function didn't take arguments.
^ That's why it isn't working.
route is undefined in the start function. Change the start function to:
function start(route) { ...

Server is running on 8080, but browser says webpage is unavailable

I followed this tutorial (http://www.hongkiat.com/blog/node-js-server-side-javascript/) and when running the next to last script (for creating a static server) the command prompt says "Server running on port 8080", but when trying to access it at localhost:8080 I just get a webpage is unavailable error.
I have made an rule in the firewall to allow access to 8080 as well.
What could be causing this? Should i be trying to access the page from another address?
When I try to access the page i get the following error message in cmd:
C:\Users\id122302\Documents\test.js:11
path.exists(full_path,function(exists)
^
TypeError: undefined is not a function
at Server.<anonymous> (C:\Users\id122302\Documents\test.js:11:7)
at Server.emit (events.js:110:17)
at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_server.js:343:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
This is my code:
var sys = require("sys");
my_http = require("http");
path = require("path");
url = require("url");
filesys = require("fs");
//Create Server
my_http.createServer(function(request,response)
{
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
path.exists(full_path,function(exists)
{
if (!exists)
{
response.writeHeader(404, {"Content-Type":"text/plain"});
response.write("404 Not Found\n");
response.end();
}
else
{
filesys.readFile(full_path, "binary", function(err,file)
{
if (err)
{
response.writeHeader(500,{"Content-Type":"text/plain"});
response.write(err + "\n");
response.end();
}
else
{
response.writeHeader(200);
response.write(file,"binary");
response.end();
}
});
}
});
}).listen(8080);
console.log("Server Running on 8080");
Your server shows an exception and a line number => go for that place !
As observed by #maniacnero, there's no more such thing as path.exists in the API. There's an fs.exists but it's been deprecated, to avoid abusive usage in node's concurrent context.
The feared scenario would be :
you check asynchronously if a file exists.
some other routine deletes/renames it in the meanwhile, or something else on the server does.
you think that the file exists so you try to open it and confidently don't handle the error case.
So the lessons learnt here are :
do things atomically
always deal with failures right away
Provided you stick to this discipline, there's no need for such thing as fs.exists. Here's a modified version of your code :
var sys = require("sys");
var http = require("http");
var path = require("path");
var url = require("url");
var fs = require("fs");
var port = 8080;
http.createServer(function(request,response) {
var my_path = url.parse(request.url).pathname;
var full_path = path.join(process.cwd(),my_path);
fs.readFile(full_path, function(err, file) {
if (err) {
response.writeHeader(404, {"Content-Type":"text/plain"});
response.write("404 Not Found\n");
response.end();
} else {
response.writeHeader(200);
response.write(file);
response.end();
}
});
}).listen(port);
console.log("Server Running on " + port);
I also removed those "binary" thingys, that are way outdated and not documented in the API either !
Playing around with sample code is a nice way to learn, but only if you don't do it blindly. ;) Especially in a weakly typed language building on a fast changing API and where myriads of tutorials have been written by utter beginners. This is your friend : https://nodejs.org/api/

Resizing image error with gm package of node js

I've been trying this for a while, but I keep getting the error:
Error: Command failed: Invalid Parameter - /images
I installed ImageMagick and the gm package, so that's definitely not the problem.
gm(imageLocation)
.resize(100) // use your own width and height
.write('here.jpg', function (err) {
if (!err) console.log(' hooray! ');
else console.log(err);
});
imageLocation being ./images/3.jpg. Why does this error keep happening? I looked at the documentation
I'm on a Windows 32 bit machine. My server is supposed to get an image from a folder, resize it, and then display it. It seems like I have to write the resized photo and then display that, but the writing process always errors out and the image ends up being empty.
If there's a way to skip the writing part and just displaying the photo directly, that would be awesome too.
Thanks!
URL Query I used: http://localhost:8123/images/3.jpg
Complete code:
var querystring = require('querystring'); //used for parsing parts of urls
url = require('url');
http = require('http');
fs = require('fs');
gm = require('gm').subClass({ imageMagick: true });;
var server = http.createServer();
server.on('request', function(request, response){
var parsed_url = url.parse(request.url, true); //true gets the query as well
imageLocation = '.' + parsed_url.pathname;
gm(imageLocation)
.resize(100) // use your own width and height
.write('here.jpg', function (err) {
if (!err) console.log(' hooray! ');
else console.log(err);
});
if (getImage('here.jpg', response)){
//image is displayed
}
else{
respond404(parsed_url.pathname, response);
}
})
function respond404(path, response){
respond(404, "The requested path " + path + " was not found", response)
}
function getImage(location, response)
{
try{
var img = fs.readFileSync(location);
response.writeHead(200, {'Content-Type':'image/jpg'}); //parse this end
response.end(img, 'binary');
return true;
}catch(e){
return false;
}
}
server.listen(8123);
The answer Svbaker put can be used in Linux (maybe Mac as well?)
For Windows I got it to work by opening the command line in administrator mode and starting my server there.
I was able to get your code to work by changing how you required gm as follows:
var gm = require('gm');
I also had to remember to execute node with the correct permissions in my case:
sudo node server.js

How to load webpage as response to request

I would like to load a webpage in response to a request for a path,I have figured it out till here:
var http=require('http');
var mysql=require('./mysql');
var fs=require('fs');
var app=http.createServer();
app.listen(8000);
app.on('request',function(req,res){
var _path=req.url;
if(path==='/')
{ res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello World \n');
}
if(path==='/demo')
{
//this is bad,we could use streams to improve this(although this is non-blocking too)
fs.readFile('../maps/google_maps.html',function(err,contents){
if(err)
{
console.log('did not work');
res.writeHead(500);
}
else
{
res.setHeader("Content-Length", contents.length);
res.setHeader("Content-Type", mimeType);
res.statusCode = 200;
res.write(contents);
}
res.end();
});
}
});
Server does not work for /demo path,it throws the error:
{ [Error: ENOENT, open '../maps/google_maps.html'] errno: 34, code: 'ENOENT', path: '../maps/google_maps.html' }
var _path=req.url;
if(path==='/')
you create _path but then compare path, which is always undefined
change it to var path=req.url;
error:
ENOENT means file not found
../maps/google_maps.html is a path relative to where you're calling node from, so make sure file exists, or specify full path
To make a path relative to the script, you must use the __dirname variable.
__dirname + '/path/to/file'
You should better use Express.js along with Jade which is a templating engine for Node.js. You can easily render html pages to client and serve static content

How to pass object parameters to functions in JavaScript

My server.js is
// server.js - the outer server loop
var http = require('http')
, php = require("./phpServer");
function start() {
function onRequest(request, response) {
php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
response.write('Ending');
response.end();
}
http.createServer(onRequest).listen(80);
console.log("Server started.");
}
exports.start = start;
That calls php.phpServer every request with response as the 3rd param.
phpServer contains.
//
// phpServer.js - a generic server to serve static files and
//
var fs = require('fs')
, pathfuncs = require('path')
, url = require('url')
, mimetypes = require('./mimetypes')
function phpServer(root, request, response) {
// serve static or pass to php.
var data = url.parse(request.url);
var ext = pathfuncs.extname(data.pathname);
fs.stat(root+request.url, function(err, stat) {
if (err || !stat.isFile()) { // error or not file.
console.log('404');
response.writeHead(404);
response.write('Not Found');
return;
}
// exists - serve.
console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
response.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
response.write('Somethign to serve');
// fs.createReadStream(root+request.url).pipe(response);
});
}
exports.phpServer = phpServer
As I see it, response is an object and is passed by reference, therefore the response.write() here should write to the response.
It doesn't. Response here is NOT the same as response in onRequest, so nothing in phpServer is sent to the browser - not code nor content.
The console.logs come out and show what I would expect.
How can I get the object response passed so I can call write on it?
------------- added later -------------------
I've tried to apply answers given and code for server.is now
// server.js - the outer server loop
var http = require('http')
, fs = require('fs')
, pathfuncs = require('path')
, url = require('url')
, mimetypes = require('./mimetypes')
function phpServer(root, request, res) {
// code adapted from page 118 of Smashing Node.js by Guillermo Rauch
// res is response provided to onRequest.
var data = url.parse(request.url);
var ext = pathfuncs.extname(data.pathname);
res.write('Start reply');
fs.stat(root+request.url, function(err,stat) {
// define delayed callback - reponse in scope
if (err || !stat.isFile()) { // error or not file.
console.log('404');
res.writeHead(404);
res.write('Not Found');
res.end
return;
};
// exists so serve.
console.log("serve("+root+request.url+", mimetypes.mimetype("+ext+"))");
res.writeHead(200, {'Content-Type': mimetypes.mimetype(ext)});
res.write('The file contents');
res.end;
} // end callback,
); // end fs.stat call.
} // end phpServer
function start() {
function onRequest(request, response) {
phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
}
http.createServer(onRequest).listen(80);
console.log("Server started.");
}
exports.start = start;
This does not reply at all - it times out. However the call to res.writeHead will either
fail, if res is out of scope/does not exist/undefined, or succeed if re is the param passed in.
It succeeds, and is followed by write and end, so please - what have I got wrong.
If the file does not exist I get a start reply and then a timeout.
At the res.write('Start reply'); res is the response param, yet it isn't later in the fs.stat call-back.
Why not?
Damn - this is frustrating.
The call to response.end should be moved from the onRequest function to phpServer. As it stands phpServer cannot write anything else since the stream has been closed.
function onRequest(request, response) {
php.phpServer('D:/websites/coachmaster.co.uk/htdocs',request, response);
// response.end(); // move this to phpServer
}
As explained in the documentation for response.end
This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete.
Your problem is not with parameter passing, it's with basic asynchronous control flow. The stat() function does not do its work immediately. Its callback parameter is called when it's done. You basically cannot structure the code the way you've done it. Instead, your "phpServer" code will need to take a callback parameter of its own, and call it after it does its work.

Categories

Resources