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

Dione Marshall
Dione Marshall
6,184 Points

Server Misunderstanding

So I do not understand the whole web host and server idea. What exactly is a web hosting service used for and what is a server that I may create with Node.js used for?

3 Answers

andren
andren
28,558 Points

A server is used to serve your webpage. Essentially when somebody connects to your webside there needs to be some software that responds to the connection and actually sends back a webpage, that is the role of the server.

Take Treehouse.com for example. When you connect to "https://teamtreehouse.com/community/" your browser will connect to Treehouse's server and ask it to give you the /community page. The server will then respond with that page.

A server can also execute code before sending a page to the person requesting it. For instance it can pull out data from a database and use that data to populate the page with data. When you go to /community for example you get a page with the most recent community posts. That page is obviously not updated every minute by hand, but instead by having the server automatically pull out the most recent posts from a database and just inserting that data into the page before it sends it to your computer.

A server is also where data is sent to when you have a form on your website for example. Since server-side code is not shared with clients you can have it do security related things which you could not do with client-side code.

I would recommend that you take a look at the How the Internet Works and How the Web Works courses from the Digital Literacy track if you are curious about how servers and the internet in general works. As those courses goes into more detail about those topics.

Dione Marshall
Dione Marshall
6,184 Points

So explain to me what web hosting is and why we need it. Do I need to create a http server in like Node.js to handle database posts or does a web host do it for you?

andren
andren
28,558 Points

A web host is a place that gives you access to a server machine that hosts your website's files and usually runs the server. Different webhosts gives you access to different things. Some do configure a server for you, usually one setup to allow you to execute PHP code on the server but not much else. Some allow you to deploy your own server code using NodeJS or something similar. And some just give you direct access to a terminal that allows you to install whatever software you want.

For simple NodeJS hosting Heroku is pretty popular as they make it easy to deploy your NodeJS application to them. But there are lots of alternatives out there.

Julio Soto
Julio Soto
4,544 Points

seems like you do not understand how the internet works. I would suggest that you research those topics. You cannot program a server if dont know what it is. A node server does what any other server does. mainly routing requests and connecting to a database or other web services. your front-end app will talk to the node server in order to retrieve or save data. research HTTP requests. and basic functionality of the internet.

Dione Marshall
Dione Marshall
6,184 Points

That wasn't my question. Do I create a server and upload it to my web hosting account or what? Why create a server when a web hosting service is a server already?

Julio Soto
Julio Soto
4,544 Points

Ok since i got the worst answer let me try a long explanation:

lets say you created a database and a node.js server program to handle the requests and send the data. when you run the node program in localhost:3000 (first hint: host) everything works just fine. You make a request with your same computer and get a response from your same computer.

What happens is that your computer (browser) sends a request to the internet so the request goes to the router in your house, then the router gets the 'localhost' address and simply sent the request right back to your same computer. When the request arrives (at your computer again) the NODE PROGRAM IS LISTENING so it takes the request and builds a response according to the way you programmed it. Then node sends that fresh response back to the client IP address. Your home router figures out that the IP address is right there in the same network so it bounces it right back to your computer.

Can you see the localhost problem? How would I personally access your node server?? I can send a request to your home IP address but upon arriving at your router IT WOULD GET LOST. your router is not prepared to do port routing. So you may get an expensive router and go through the difficult process of configuring it to is ready to route request to the port connected to your node program. ooohh but wait! what if you turn off your computer?? The request would fail again. so you may choose to keep your computer powered on 24/7 ooh but wait you are getting way too many requests and clients are uploading and retrieving hundreds of pictures mmm your meager laptop cannot handle such load. You are gonna have to get a new computer with lots of hard drive space, memory, proccesor etc..

So is that it? is that all you need to HOST your node server and database?? nope, there is another problem. Your neighborhood network is designed to handle certain amounts of traffic. When you host from your home you may create lots of extra traffic and that may seriously screw up the neighborhood network. Have you seen the movie 'The Social Network' ? that is how he choked the Harvard network. Be prepared to hear from your internet service provider, they will take action against you and even sue you. The contract you signed with them may specifically prohibit home web servers.

So how do you solve these hellish problems? well, there is a very simple solution. Do not host from home. RENT A COMPUTER THAT CAN DO ALL THE THINGS MENTIONED. For $10 a month you can have full use of a computer that has been setup to work as a web server. It is connected to powerful routers that you don't have to configure. It is guaranteed to stay on 24/7, It can transfer data very fast, It has huge hard drives, and it is sitting on a network that can handle a vast amount of traffic. That computer is far away from you but that is no problem because you can access it remotely from your home.

A Web Hosting company is nothing more than a huge room with 1000 computers ready to be rented out to people like you. People who need to HOST their node.js or PHP server programs and SQL databases. A 'Web Host' is just a computer. Give it any fancy name you'd like, is just a computer. One that has been setup and optimized to act as a Web Server, and you can access it remotely.

With that said. let me ask you, What would this rented 'Web Host' computer do? what would it serve?? a webpage? what flippin webpage?? a database?? what flippin database?? the thing IS JUST A COMPUTER. It would not serve or do anything at all. I would not even respond. Just like a brand new computer from best buy, it powers on and runs an operating system. but the is NO SOFTWARE IN IT. If you want to play a game.....you must download an install the game first right? Likewise, your rented 'Web Host' computer simply runs a server operating system (not windows) and there is nothing in it. THAT IS WHERE YOU COME IN WITH YOUR PROGRAMS you upload and configure your node.js server program along with your database and any other resource you want. Actually, you could even use it as personal storage if you want. After all IS JUST A COMPUTER so create a random folder in it and throw some personal files in it. Nobody will know and nobody will care.

After this long explanation I hope you can see the difference between the 'server' program you create and the 'server' computer you rent. I feel the term 'server' gets thrown around in a generic way and it creates confusion.Please ask if did not make it clear or made some weird mistake.