Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Make A Basic Database (nodejs)
JakeHu2020 (39)

How To Make A Database

So you came to learn how to make a database? If you have, your probably someone who just can't manage to add things from Nodejs to your html page.

So Lets Start Anyways:


Firstly, create a text file called server.txt. Make sure not to put it in any files! In server.txt type in {} so it can store multiple named items.

Secondly, open index.js Or Your NodeJS file. You will need socket.io, fs, express, and http. This will be all the code you need for the database (You don't need to do some of it if you already wrote it.

So now we have the imports! If you made a file called public and added all the html there, add at the bottom of the code

or If you just plainly made index.html file, add

So now that we made our setups, lets move on to the actual database coding.

Editing + Reading server.txt

In this tutorial, we use the fs import to edit files. First in index.js lets create a variable called db, then assign it to {}. Now to add the code to get the content in server.txt. Your code in this section should be this:

Now the db variable will always be the text in server.txt. Now for the editing-server function. Lets name it addData. In this, we'll use the fs.writeFile function.

Now lets add some remove Data and clear all text functions.

Sending Functions To HTML Page

Create a file called io.js. Make sure your html file can access it!

So now that we have the file ready, lets now send the function and database through socket io.

Open up your html page and add:

now in io.js, lets add the basic starts:

Go Back in your nodejs file. Now we can create new socket events. We'll use socket.emit. The syntax for socket.emit is

Replace the <EVENT_NAME> with your event name, and <EVENT_DATA> With the data you want to send. You should also put all the io.emit's inside an io.on('connection') function. So lets put this io.emit things together:

TIP: if you want to change data, you can just remove it, and re-add it while changing to the value you want.

So now that we have the events created, it's time to locate the events on our io.js file. To get the data, we'll use socket.on to get the data:

So now if you open you can change them successfully on your html page. The BEST part of it is that it stays, even when you exit of the page.


So now you have your database ready! your code should like like this (you don't need the comments):

index.js (nodejs file)

index.html (the only needed parts)

io.js (socket.io getter)

That's it!

Your welcome, and have a nice day!

CodeLongAndPros (1644)

That's it! Also, if you ask "what if hackers can come in and edit it through console?", lets just assume or at least hope there are no hackers around here.

Your definition of hacker is wrong.
Please read this http://www.catb.org/jargon/html/meaning-of-hack.html

CodeLongAndPros (1644)

You could say "computer crackers" or even "bad actors".
Also, the Jargon File was written in the 90's, with the earliest version dating to 1977. Simply, the media has using this word wrong for three decades.