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
Andrew Young
Courses Plus Student 639 PointsWhy 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]);
Andrew Young
Courses Plus Student 639 PointsSteven, 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
Andrew Young
Courses Plus Student 639 PointsHere 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
243,318 PointsAt 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];
Andrew Young
Courses Plus Student 639 PointsHow about can you find where do they restrict that every chat room can only have two user in it?
Steven Parker
243,318 PointsI'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".
Steven Parker
243,318 PointsSteven Parker
243,318 PointsDo you have this set up in a workspace or other REPL so it can be tested without downloading and installing?