0

I'm having problem with the call of some function, I'm learning JavaScript and NodeJS with the knowledge I get from JavaScript, I don't see the clue why I have this error, the function I'm trying to do is this one:

function isFileView(dat)
{
    var file = dat;

    //Check if im gettint the right data.
    console.log('file is ' + dat);
    var extension = file.lastIndexOf('.') + 1;

    if(extension != 0)
    {
        switch (extension)
        {
            case 'html':
                return file;
                break;

            case 'css':
                return file;
                break;

            case 'js':
                return file;
                break;
        }
    }
    else
    {
        file = file + '.html';
        return file;
    }
}

The dat value returns as undefined and is making my server to stop. I've changed the parameter of the function and the variables inside the function but no clue. This is the whole script I've made so far:

//Load modules
var http = require('http');
var fs = require('fs');
var url = require('url');

//Send message to console.
console.log('starting.');

//Load config JSON file and 
var content = fs.readFileSync("config.json");
var config = JSON.parse(content);
var server_ip = config.server_ip;
var port = config.port;

console.log('Started.');

//Create server.
server = http.createServer(function(request, response)
{
    //if url has GET method.
    if(request.url.lastIndexOf('?') != -1)
    {
        //resolve url only view.
        var view = getUrlView(request.url);

        console.log('requested: ' + view);
        console.log(methodGet(request.url));

        //Get requested page
        fs.readFile('./views' + view + '.html', function(error, data)
        {

            //if error returned, send error message
            if(error)
            {
                response.writeHead(400, {'Content-type': 'text/plain'});
                response.end('Page was not found');
            }

            //if error was not returned, send the html
            else
            {
                response.writeHead(200, {'Content-type': 'text/html'});
                response.end(data);
            }
        });

        if(view == '/')
        {
            response.writeHead(200, {'Content-type': 'text/plain'});
            response.end('Hello world from node.js');
        }
    }

    else
    {
        console.log('requested: ' + request.url);
        isFileView(view);

        //Get requested page
        fs.readFile('./views' + request.url + '.html', function(error, data)
        {

            //if error returned, send error message
            if(error)
            {
                response.writeHead(400, {'Content-type': 'text/plain'});
                response.end('Page was not found');
            }

            //if error was not returned, send the html
            else
            {
                response.writeHead(200, {'Content-type': 'text/html'});
                response.end(data);
            }
        });

        if(request.url == '/')
        {
            response.writeHead(200, {'Content-type': 'text/plain'});
            response.end('Hello world from node.js');
        }
    }

});
server.listen(port);

function getUrlView(dat)
{
    //resolve data.
    var url = dat.lastIndexOf('?');
    url = dat.slice(0, url);

    return url;
}

function methodGet(dat)
{
    //resolve get method (requires url module).
    var url_parts = url.parse(dat, true);
    return url_parts.query;
}

function isFileView(dat)
{
    var file = dat;

    //Check if im gettint the right data.
    console.log('file is ' + dat);
    var extension = file.lastIndexOf('.') + 1;

    if(extension != 0)
    {
        switch (extension)
        {
            case 'html':
                return file;
                break;

            case 'css':
                return file;
                break;

            case 'js':
                return file;
                break;
        }
    }
    else
    {
        file = file + '.html';
        return file;
    }
}

I'm beginner at JavaScript so with NodeJS, but I'm an experienced programmer in PHP so I don't know if the switch works in that way.

6
  • "...the dat value returns as undefined" Since dat is a function parameter, you need to follow the path backward to see where it is being called, and why it is being called with undefined instead of the value you expect. Commented Apr 3, 2014 at 20:49
  • extension seems to be an integer, but in the switch you are looking for a string..?! add a default: case and see if it enters there Commented Apr 3, 2014 at 20:50
  • What is the line of the error ? Commented Apr 3, 2014 at 20:50
  • the error was already shown in var extension, returning that cannot call method LastIndexOf undefined, so the server stops right there. and im sorry about newbie code, im new at this, and about the dat parameter, i dont see why it gets the error if i made it in the same way with other functions. Commented Apr 3, 2014 at 20:53
  • @nosthertus The problem is where it's called -- isFileView(view);. dat's value will be determined by view and view isn't being given a value within the else block. Commented Apr 3, 2014 at 20:55

1 Answer 1

1

The problem appears to be related to how you're defining the view variable which you're passing as an argument to your isFileView() function...

    //if url has GET method.
    if(request.url.lastIndexOf('?') != -1)
    {
        //resolve url only view.
        var view = getUrlView(request.url);

        ....
    }
    else {
        ....
        isFileView(view); // << view isn't defined here since we never were in the 'if` statement above
        ....
    }

So, what you'll need to do is either define the variable before the if statement or inside your else statement or reset your function to check for an undefined argument so it doesn't cause any errors.

else {
    var view = getUrlView(request.url);
    isFileView(view); // << view isn't defined here since we never were in the 'if` statement above
    ....
}

Or...

function isFileView(dat)
{
    var file = dat || false;

    if(file) {
       ...
    }
}
Sign up to request clarification or add additional context in comments.

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.