Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js Beginner - Can't get past nodetuts tutorial number 2 - stdout and child process

Node.js Beginner - Can't get past nodetuts tutorial number 2 - stdout and child process

Problem

I was trying to go through the tutorial nodetuts.com - tutorial 2 and unfortunatly could not get the example working, I'm very new to node.js and going through any tutorials I can get hold of. I understand that node.js is still beta and I figured that the code that makes this work is now obsolete. (This is the code):

var http = require('http');
var spawn  = require('child_process').spawn;

http.createServer(function(request, response){

    response.writeHead(200, {
        'Content-Type' : 'text/plain'
    });

    var tail_child = spawn('tail', ['-f', 'test.txt']);

    tail_child.stdout.on('data', function(data){
        console.log(data.toString());
        response.write(data);
    });


}).listen(4000);

Anyway, determined to continue on I have been looking through the documentation on the node website and found this: http://nodejs.org/api/all.html#all_child_pid This is not exactly what I want (I want to complete that tutorial linked to up top) but I wanted to get something todo with child process working and incorperated that code into this:

var http = require('http');

var server = http.createServer(function(res, req){
    res.writeHead(200);
    res.end('testing');


    var spawn = require('child_process').spawn,
        grep  = spawn('grep', ['ssh']);

    console.log('Spawned child pid: ' + grep.pid);
    grep.stdin.end();

}).listen(4000);

Unfortunately when I refresh the page http://localhost:4000/ I get nothing and command prompt spits out: (I know it says writeHead is a problem, but it works fine in other examples - (like nodetuts - tutorial 1))

        res.writeHead(200);
            ^
TypeError: Object # has no method 'writeHead'
    at Server. (Z:\Joseph Goss Folder\Google Drive\Code\javascript_first\nodejs_first\stdoutTest.js:20:6)
    at Server.EventEmitter.emit (events.js:91:17)
    at HTTPParser.parser.onIncoming (http.js:1785:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)
    at Socket.socket.ondata (http.js:1682:22)
    at TCP.onread (net.js:404:27)

I wonder why I can't get this working, I'm obviously missing something but I don't know what, and I can't even get past tutorial number 2. :(

  • I'm running windows 7
  • I have also looked at this code Why does response.write appear to block my browser in Node.js? and his code doesn't work at all either.
Problem courtesy of: Joseph

Solution

You interchanged req and res in the function passed to createServer

var http = require('http');

var server = http.createServer(function(req, res){
    res.writeHead(200);
    res.end('testing');


    var spawn = require('child_process').spawn,
        grep  = spawn('grep', ['ssh']);

    console.log('Spawned child pid: ' + grep.pid);
    grep.stdin.end();

}).listen(4000);
Solution courtesy of: Pavan Kumar Sunkara

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js Beginner - Can't get past nodetuts tutorial number 2 - stdout and child process

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×