Printer.js无法打印到控制台

时间:2016-02-27 18:01:07

标签: javascript node.js

因此出于某种原因,在我将打印功能移动到他们自己的文件并尝试在我的app.js文件上运行node命令之后突然间,它不想在我的控制台中打印任何内容。那是为什么?

我仍然在努力调试这个,因为当我运行以下命令时没有打印出来。

node app.js myusernamehere

有人有任何想法吗?

app.js

var profile = require("./profile.js");
var users = process.argv.slice(2);

users.forEach(profile.get);

profile.js

//Problem: We need a simple way to look at a user's badge count and Javascript points
//Solution: Use Node.js to connect to Treehouse's API to get profile information to print out
var https = require("https");
var http = require("http");
var printer = require("./printer.js");

function get(username) {
    //Connect to API URL (http://teamtreehouse.com/username.json)
    var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
        var body = "";
        //Read the data
        response.on('data', function(chunk) {
            body += chunk;
        });
        response.on('end', function() {
            if (response.statusCode == 200) {
                try {
                    //Parse the data
                    var profile = JSON.parse(body);
                    //Print the data
                    printer.printMessage(username, profile.badges.length, profile.points.JavaScript);
                } catch(error) {
                    //Parse Error
                    printer.printError(error);
                }
            } else {
                //Status Code Error
                printer.printError({message: "There was an error getting the profile for " + username + ".  (" + http.STATUS_CODES[response.statusCode] + ")"});
            }
        });
    });

    //Connection Error
    request.on('error', printer.printError);
}

module.exports.get = get;

printer.js

//Print out message
function printMessage(username, badgeCount, points) {
    var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript";
    console.log(message);
}

//Print out error messages
function printError(error) {
    console.error(error.message);
}

module.exports.printMessage = printMessage;
module.exports.printError = printError;

1 个答案:

答案 0 :(得分:0)

我不知道实际发生了什么,但它现在有效,我的代码中没有任何内容被更改。