0

I am trying to display an image on a basic web page on a localhost w/ port 5000 here is main.js

var http = require('http');

var domain = require('domain');

var root = require('./root');
var image = require('./image');



function replyError(res) {

  try {

    res.writeHead(500);

    res.end('Server error.');

  } catch (err) {

    console.error('Error sending response with code 500.');

  }

};



function replyNotFound(res) {

  res.writeHead(404);

  res.end('not found');

}



function handleRequest(req, res) {

  console.log('Handling request for ' + req.url);

  if (req.url === '/') {

    root.handle(req, res);

  } else if (req.url === '/image.png'){

    image.handle(req, res);

  } else {

    replyNotFound(res);

  }

}



var server = http.createServer();



server.on('request', function(req, res) {

  var d = domain.create();

  d.on('error', function(err) {

    console.error(req.url, err.message);

    replyError(res);

  });

  d.run(function() { handleRequest(req, res); });

});



function CallbackToInit(){

  server.listen(5000);
};
root.init(CallbackToInit);

Using callbacks I want the server to start listening(5000) only after the init function of the following code runs

var http = require('http');

var body;

exports.handle = function(req, res) {
  res.writeHead(200, {
    'Content-Type': 'image/png'
  });
  res.end(body);
};

exports.init = function(cb) {
  require('fs').readFile('image.png', function(err, data) {
    if (err) throw err;
    body = data;
    cb();
  });
}

It's an assignment I can't use express

I am trying to get image.png to be displayed, I think body = data doesn't work because it can't hold an image like a string? I don't want to put any HTML into my js file.

7
  • You haven't actually asked a question. Is there something that's not working how you'd expect? An error? Commented Jan 31, 2014 at 23:10
  • yeah image.png isn't displaying. I assume because body can't hold an image like it does a string?? in the developer tools it's not even listed as a resource Commented Jan 31, 2014 at 23:14
  • 1
    What if you directly navigate to image.png? Can you try changing the content-type? The text/html probably isn't helping. image/png would be appropriate, I believe. Commented Jan 31, 2014 at 23:19
  • oh it's already on image/png I forgot to change that before I posted sorry. I would prefer not to res.write(html for image). I'm not supposed to use html code Commented Jan 31, 2014 at 23:34
  • oh wait it looks like my handle function isn't firing oO Commented Jan 31, 2014 at 23:49

2 Answers 2

1

Don't roll your own app server. Use one of the great web app frameworks like express or connect.

var express = require('express');
var app = express();

app.use(express.logger());
app.use(express.static(__dirname + '/public'));
app.listen(process.env.PORT || 5000);

Trust me, this is better.

Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at the node.js example for a simple http server or a tutorial/example, such as this, for serving static files through a simple server.

2 Comments

well i have error statements but my shit does get fired I adde console.log to the functions and the init and handle functions are being fired
I must've missed some of your code then, my apologies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.