julian
julian

Reputation: 4714

node.js - how do i actually get request?

I'm asking a lot questions lately ;)
So I made some progress in creating my web chat.

So I got it to work,
My question was, How to type something into the textarea, and after click enter the message will be logged into the server?

I updated the code, so it works.

The node.js file -

var http = require('http');
var fs = require('fs');
var path = require('path');

var messages = [];

// Simple Function to Load all of the HTML/CSS/JavaScript Files.
function LoadHTML(html, requrl, res) {
    var filePath = '.' + requrl;
    if (filePath == './') {
        filePath = './' + html;
    }

    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
        break;
        case '.css':
            contentType = 'text/css';
        break;
    }

    fs.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    res.writeHead(500);
                    res.end();
                }
                else {
                    res.writeHead(200, { 'Content-Type': contentType });
                    res.end(content, 'utf-8');
                }
            });
        }
        else {
            res.writeHead(404);
            res.end();
        }  
    });  
}

var server = http.createServer(function (req, res) {

    var url = req.url;

    LoadHTML('index.html', url, res);

    if(url.substring(0,16) == "/ser.js?message=") {
        var message = url.substring(16, url.length);
        console.log(message);
    }

}).listen(8125);

Here is the HTML/CSS/JavaScript Code - http://jsfiddle.net/Q79PK/1/

EDIT: I got it to work, I updated the code, You can look and learn ;)
Up votes will be great!

Upvotes: 0

Views: 304

Answers (1)

Fafhrd
Fafhrd

Reputation: 436

You should not use http.get(options, callback) as it is designed to perform a HTTP GET without data. Instead use http.request(options, callback) in which you will be able to add data to the request. See: http://nodejs.org/api/http.html#http_http_request_options_callback

For server-side logging and handling data posted within the request, make the netire req object to be the parameter of the loadHTML function (and not req.url). Then you will be able to add a listener to the ServerRequest:

req.addListener("data", function(data) {
    console.log("data : "+data);
});

(and update var filePath = '.' + req; accordingly to var filePath = '.' + req.url; ).

You can test this by simulating the request using the cURL command if you have it :

curl -d "some data" http://localhost:8125

and server-side you have the following logging:

data : some data

Here is the full modifications of the code that I made :

var http = require('http');
var fs = require('fs');
var path = require('path');

var messages = [];

// Simple Function to Load all of the HTML/CSS/JavaScript Files.
function LoadHTML(html, req, res) {
    var filePath = '.' + req.url;
    if (filePath == './') {
        filePath = './' + html;
    }

    req.addListener("data", function(data) {
        console.log("data : "+data);
    });

    var extname = path.extname(filePath);
    var contentType = 'text/html';
    switch (extname) {
        case '.js':
            contentType = 'text/javascript';
        break;
        case '.css':
            contentType = 'text/css';
        break;
    }

    fs.exists(filePath, function(exists) {

        if (exists) {
            fs.readFile(filePath, function(error, content) {
                if (error) {
                    res.writeHead(500);
                    res.end();
                }
                else {
                    res.writeHead(200, { 'Content-Type': contentType });
                    res.end(content, 'utf-8');
                }
            });
        }
        else {
            res.writeHead(404);
            res.end();
        }  
    });  
}

var server = http.createServer(function (req, res) {
    LoadHTML('./index.html', req, res); // Calling the Function to load all of the files.
}).listen(8125);

HTH

Upvotes: 1

Related Questions