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 trial

JavaScript

Why my chat room only allow number id but not alphabet

I download a code from here

As I playing with it I found out you can create custom id by just typing the id you want after /chat/(here)

But then without trying number, I tried alphabet but it seems doesn't work, can anybody help me?

Also I've found the following code that I think it might restrict the id but don't know how to fix it to allow alphabet

// getting the id of the room from the url
var id = Number(window.location.pathname.match(/\/chat\/(\d+)$/)[1]);
Steven Parker
Steven Parker
243,318 Points

Do you have this set up in a workspace or other REPL so it can be tested without downloading and installing?

Steven, I've download it extracts on my E disk on my PC, run npm install in cmd, and run the file app.js with node app.js on Windows 10 with node version v6.11.2. And with its own demo running on Heroku has the same issue chatroom id can only have numbers

Steven Parker
Steven Parker
243,318 Points

For sharing purposes, you might consider creating a workspace, uploading into it, and then make a snapshot and post the link to it here.

Here is the workspace

For the storage reason I delete the node_module directory you'll need to run npm install locally to get them back

1 Answer

Steven Parker
Steven Parker
243,318 Points

At first glance, I noticed this on line 6 of chat.js:

    var id = Number(window.location.pathname.match(/\/chat\/(\d+)$/)[1]);

The regex here is restricting the pathname to numbers only, and then the returned result is converted to a number. You could change this behavior but you might need to check the rest of the code carefully to see if there might be some other areas that expect "id" to be numeric and don't handle it correctly as a string. But here's the modified parser:

    var id = window.location.pathname.match(/\/chat\/(\w+)$/)[1];

How about can you find where do they restrict that every chat room can only have two user in it?

Steven Parker
Steven Parker
243,318 Points

I'm not familiar with socket.io, but I see in chat.js there's a function that handles "peopleinchat" events from the socket on line 59. There's code to handle data.number values of 0 and 1, but for any other value it shows a message of "tooManyPeople". Assuming that's not a restriction of the socket itself, you could try eliminating the current "else" block at line 123 and change line 92 to be a plain "else".