var inside函数未定义(JavaScript,NodeJS)

时间:2014-04-03 20:46:57

标签: javascript node.js

我对某些功能的调用有问题,我使用JavaScript获得的知识学习JavaScript和NodeJS,我不知道为什么我有这个错误的线索,我试图做的就是这个:

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;
    }
}

dat值返回undefined并使我的服务器停止。我已经改变了函数的参数和函数内部的变量,但没有任何线索。这是我到目前为止制作的整个剧本:

//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;
    }
}

我是JavaScript的初学者,所以使用NodeJS,但我是一位经验丰富的PHP程序员,所以我不知道交换机是否以这种方式工作。

1 个答案:

答案 0 :(得分:1)

问题似乎与您定义view变量的方式有关,该变量是您作为isFileView()函数的参数传递的......

    //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
        ....
    }

因此,您需要做的是在if语句之前或在else语句中定义变量,或者重置函数以检查undefined参数,以便它不会导致任何错误。

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

或者...

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

    if(file) {
       ...
    }
}