2

I am a Java developer learning Javascript (Node.js).

This is the first piece of code I tried running :

var sys = require("sys"),
my_http = require("http");
my_http.createServer(function(request,response){
    response.writeHeader(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}).listen(8080);

IF there was no documentation, how would have I known that createServer takes a function which takes request and response as parameter ? I am asking this because I want to prepare myself for all the undocumented code I will start facing soon. Here is the source for createServer function :

function createServer(options) {
    var bunyan = require('./bunyan_helper');
    var InternalError = require('./errors').InternalError;
    var Router = require('./router');
    var Server = require('./server');

    var opts = shallowCopy(options || {});
    var server;

    opts.name = opts.name || 'restify';
    opts.log = opts.log || bunyan.createLogger(opts.name);
    opts.router = opts.router || new Router(opts);

    server = new Server(opts);
    server.on('uncaughtException', function (req, res, route, e) {
        if (this.listeners('uncaughtException').length > 1 ||
            res._headerSent) {
            return (false);
        }

        res.send(new InternalError(e, e.message || 'unexpected error'));
        return (true);
    });

    return (server);
}

I understand Javascript is a dynamically typed language, but wondering how do people debug or understand each other's code without knowing types.

2
  • 3
    That's what documentation, comments, and explicit variable names are for. Unfortunately, you're right - options doesn't scream function to me. It would seem you'll need to look at Router or Server and see how they're using options. It might clearer there. Also, http is a pretty standard node module. Documentation online seems alright. nodejs.org/api/http.html#http_http_createserver_requestlistener Commented Feb 17, 2015 at 5:34
  • Beware of wherever you got that code. The sys module has been gone for a very long time. Commented Feb 17, 2015 at 8:26

2 Answers 2

2

Well the nice thing about javascript is it's interpreted meaning you always have access to the actual source code itself. For node, you can look in node_modules/blah to read the source, but the vast majority of what is on npm is also open source on github and you can read the source there, too.

In the browser the developer tools has an auto-format button if you encounter minified code, but in node usually you don't need that as code is published unminified.

That said, some things are documented well, sometimes documentation is wrong or out of date, and sometimes reading the source code is neither quick nor straightforward. But if something is really problematic for you and is both undocumented and hard to read, you can and should switch to something else on npm because "ain't nobody got time for that".

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

Comments

0

you must be very familiar with the api when using JavaScript.for example, document.getElementById(id). There is no hint in what the id is in the code,but it is well understood.

Comments

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.