Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMarcos Cabrini Riani dos Reis
12,253 Pointsconst commonHeaders = {'Content-Type', 'text/html'};
When trying to use a common header variable, my code won't work. Erro message in the console. const commonHeaders = {'Content-Type', 'text/html'}; ^
SyntaxError: Unexpected token , at new Script (vm.js:74:7) at createScript (vm.js:246:10) at Object.runInThisContext (vm.js:298:10) at Module._compile (internal/modules/cjs/loader.js:670:28) at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10) at Module.load (internal/modules/cjs/loader.js:612:32) at tryModuleLoad (internal/modules/cjs/loader.js:551:12) at Function.Module._load (internal/modules/cjs/loader.js:543:3) at Module.require (internal/modules/cjs/loader.js:650:17) at require (internal/modules/cjs/helpers.js:20:18)
My code down here
const Profile = require('./profile.js'); const renderer = require('./renderer.js');
const commonHeaders = {'Content-Type', 'text/html'}; // Handle HTTP route GET / and POST / i.e. Home
function home(request, response) { // if url == "/" && GET if (request.url === '/') { // show search response.statusCode = 200; response.setHeader(commonHeaders); renderer.view('header', {}, response); renderer.view('search', {}, response); renderer.view('footer', {}, response); response.end(); }
// if url == "/" && POST // redirect to /:username
}
// Handle HTTP route GET / :username i.e. /chalkers function user(request, response) {
// if url == "/..." const username = request.url.replace('/', ''); if (username.length > 0) { response.statusCode = 200; response.setHeader(commonHeaders); renderer.view('header', {}, response);
// get json from teamtreehouse
const studentProfile = new Profile(username);
// on "end"
studentProfile.on('end', function (profileJSON) {
// show profile
//Store the values which we need
const values = {
avatarUrl: profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges.length,
javascriptPoints: profileJSON.points.JavaScript
};
// Simple response
renderer.view('profile', values, response);
renderer.view('footer', {}, response);
response.end();
});
// on "error"
studentProfile.on('error', function (error) {
// show error
renderer.view('error', {errorMessage: error.message}, response);
renderer.view('search', {}, response);
renderer.view('footer', {}, response);
response.end();
});
}
}
module.exports.home = home; module.exports.user = user;
1 Answer
Gabbie Metheny
33,778 PointsI ran into the same issue, since Andrew is using writeHead()
, and we're both using Node's current recommendation of setHeader()
. writeHead()
takes an object (i.e. writeHead({'Content-Type': 'text/html'})
), but setHeader()
takes two comma-separated parameters (i.e. setHeader('Content-Type', 'text/html')
). So you can't just copy and paste them into a variable like Andrew did in the video.
Since we have two strings, it probably makes the most sense to put them in an array, but they still need to be separated out once you put them to use in setHeader()
. You could do this using indexing, but the cleanest way would be to use the ES6 spread operator. Hope you can get it to work!
Thomas Johnson
7,211 PointsAs Gabbie mentions, the spread operator works great if you're keen on completing the app using setHeader().
Here's my router.js code for those that are still stuck. Remember the strings need to be inserted into an array and the commonHeaders variable need to be called preceded by 3 dots.
var Profile = require("./profile.js"); var renderer = require("./renderer.js");
var commonHeaders = ['Content-Type', 'text/html'];
//Handle HTTP route GET / and POST / i.e Home function home(request, response) { //if url === "/" && GET if (request.url === "/") { //show search
response.statusCode = 200;
response.setHeader(...commonHeaders);
renderer.view("header", {}, response);
renderer.view("search", {}, response);
renderer.view("footer", {}, response);
response.end();
}
}
function user(request, response) { var username = request.url.replace("/", "");
if(username.length > 0) {
response.statusCode = 200;
response.setHeader(...commonHeaders);
renderer.view("header", {}, response);
//get json from treehouse
var studentProfile = new Profile(username);
// on "end"
studentProfile.on("end", function(profileJSON) {
// show profile
// store the values which we need
var values = {
avatarUrl: profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges.length,
javascriptPoints: profileJSON.points.JavaScript
};
// simple response
renderer.view("profile", values, response);
renderer.view("footer", {} , response);
response.end();
});
// on "error"
studentProfile.on("error", function(error){
// show error
renderer.view("error", {errorMessage: error.message} , response);
renderer.view("search", {}, response);
renderer.view("footer", {}, response);
response.end();
});
}
}
module.exports.home = home; module.exports.user = user;
Steven Parker
231,275 PointsSteven Parker
231,275 PointsWhen posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Â Or watch this video on code formatting.
Even better, make a snapshot of your workspace and post the link to it here.