Ask coding questions

← Back to all posts
Can I use localhost?
h
Baconman321 (1104)

I am using this repl for a test, however I want to apply it to a project. I want to know what exactly remote and local databases are. I also want to know, if someone else from another computer enters information in, and the code says to store it in localhost, can I access that. Like, when they say localhost, does that mean only I can add information to it through the web browser, or does it mean it is hosted on my computer and people can still add information to it?

Answered by eankeen (2246) [earned 5 cycles]
View Answer
Comments
hotnewtop
eankeen (2246)

hey,
so a database, in essence, is basically just a server that performs a service. you send the server a request (ex. to store data), and it does the request (actually stores data), and it returns a response (ex. data stored successfully). my answer is general to any type of server, but this absolutely applies to database servers.

it seems you are wondering what controls these 'requests' to the server? can anyone send a 'request' to the server from 'anywhere'?

this is the job of the operating system / kernel to manage. it controls who can send requests to certain servers. in order for the operating system to manage this, your server must tell the operating system the relevant information...

this is how your server tells the operating system where it can be reached:

when you start a server, you are starting a process (ex. nodejs, python). whether you use nodejs or python, you call some function that tells the operating system 'hey, i'm a server, if anyone wants to send me a request, i am available at this host and this port.

for python flask this looks something like

app.run(host='0.0.0.0', port=80)

for nodejs this looks something like

server.listen(80, '0.0.0.0')

once you perform these method calls, the operating system knows that it can reach Baconman321's server at port 80 at host 0.0.0.0

but what is localhost, 127.0.0.1, and 0.0.0.0? back to your original question...

they are all addresses you can use to tell the operating system / kernel from where your server can be accessed from.

  • by specifying localhost or 127.0.0.1, you tell the operating system that only your local computer can access the server
  • by specifying 0.0.0.0, you tell the operating system that anyone (including your computer, and external computers) can access the server

for example, let's say you start your server with app.run(host='127.0.0.1', port=80), and let's say i can reach your server by going to baconman321.com. if i try to send a request to baconman321.com, the request moves through the internet, and ends up at your computer. your kernel is the first to see it and says "wow, someone made a request to the sever at port 80! OHH, but the server is running on the address 127.0.0.1. this means that it should be only accessible on the local computer, not from the internet". the operating system then sends a response telling them about the error

in the exact same scenario, except if your used the address 0.0.0.0 instead, the operating system will see that your application wants to be accessed by an external computer, and will route that request to the application. the application handles the request, does the request (ex. stores data if it is a database), and returns the response to the operating system. the operating system then returns the response to the external server.

so, if you want to create a user in your (traditional) database, this is how it would work:
a user would send a request to mywebsite.com. mywebsite.com has a server (ex. python, nodejs) that responds with html to your web browser. you sign up for an account, which sends a request to their server (ex. python, nodejs), saying "please register this user". your python or nodejs server will send another separate request to the database server saying "actually store this user with this information".

oopsies, but pouchdb

but what i missed until just right now, is that you are using pouchdb, which is totally different

pouchdb is a database, but it does not run a server. it's essentially javascript code that uses some web apis to make it look like you are talking to a traditional database. so localhost and 0.0.0.0 are less relevant here.

so if you want to create a user in a pouchdb database, this is how it would work:
a user would send a request to website.com. mywebsite.com has a server (that responds with html to y our web browser. you sign up for an account, which calls a couchdb function, which stores the user with their information somewhere on the user's local machine.

here is some information on the docs that mention using 127.0.0.1:5984...

but 127.0.0.1:5984 is the address of the couchdb server. a couchdb is basically a traditional server, and they only mention it, if you want to sync your pouchdb database with couchdb (if the pouchdb javascript (in your web browser) is running on the same computer as coucbdb, it can access it at 127.0.0.1:5984.

actual answer

so... i guess your question was about couchdb this whole time oof i think i was answering the wrong question, but to actually answer your question

Like, when they localhost, does that mean only I can add information to it through the web browser, or does it mean it is hosted on my computer and people can still add information to it?

so because we are using pouchdb, it doesn't make as much sense to talk about it in terms of localhost and 127.0.0.1. pouchdb is stored on the user's computer. so if you have 100 users, you have 100 pouchdb databases. each database only stores the info of that users, and one user cannot access the information of another user. so no, it is not hosted on your computer and people cannot add information to it, if you only using pouchdb

eankeen (2246)

i'm sorry for the wall of text, but i think only the last section 'actual answer' answers your question. maybe the first few parts can help with understanding of localhost and stuff. lemmie know if you have any questions :)

Baconman321 (1104)

@eankeen Thanks! That was really in-depth! Ok, so I am making a feedback page, and I just make it so it stores user input into a database that I can access later, no login pages. I really have a hard time learning this, so I'll just ask the question: Can I have other users store user input into a database, without having to add people to the database manually, or what exactly do I have to do? Also, if you work with PouchDB, can I have some help with that too? I am trying to add information, like have one document, and then add information to that document (in this repl, it's the test variable). I know it would be easier to add a new document, but if I add a new document, it will basically have the same name and id. Example: I make a var "test" and add it to a document, but if a user enters input, it will create a new document with the same name "test" so will the computer get mixed up? Or can I add documents with the same name? If not, then how do I make it so it adds a different name to the documents?

JustinBannon (1)

Eankeen’s post was so handy!! I quickly just changed “localhost” to 0.0.0.0 in my code and it popped with a little browser window right inside the .py replit.com project. Thanks!

[deleted]

Localhost is an inbuilt http address that a web server on your computer can use to serve web pages/assets from. It cannot be accessed from outside your computer. Often you can use this IP address, 127.0.0.1, as well as it maps to localhost.

Read this article: https://en.wikipedia.org/wiki/Localhost

CodeLongAndPros (1632)

@malvoliothegood If you look in the /etc/hosts file, you'll find that localhost is actually a DNS address defined before 8.8.8.8/8.8.4.4/1.1.1.1