Node JS datastores
Hello today I have written a simple module to allow you to host your own datastore server.
It is small and simple to use and set up.
Prerequisites
On your server you need to be have node installed.
If you are running a Debian or Ubuntu linux based server install this:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
This will install the base NodeJS which is required to run the script on the server.
Then what you need to do is install NPM, which is NodeJS’s package manager.
sudo apt install npm
That will install NPM.
Now you need to install the MySQL package with NPM.
sudo npm install mysql
Now you need to install and setup MySQL.
If you want to set up the cli to create the database and the table, I am guessing you already have MySQL set up and have a basic knowledge of MySQL.
Follow this article on how to install a LAMP stack which will provide you with MySQL and PHPmyAdmin: Here
Now you have installed PHPMyAdmin go to the homepage and log in.
Go to the SQL tab (2nd from the left on the top) and copy and past the following into the text input.
CREATE TABLE `store` (
`dataKey` varchar(50) NOT NULL,
`value` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `store`
ADD PRIMARY KEY (`dataKey`);
This will create the Database and table for you.
Brilliant! You are ready to get the files set up on your server.
Setting up
Now you need to make a file called index.js
and open it.
Paste the following into the file.
var http = require('http');
var url = require('url');
var mysql = require('mysql');
const hostname = 'gsck.co.uk'; // REPLACE WITH YOUR DOMAIN
const port = 1234;
var con = mysql.createConnection({
host: "localhost", // YOUR HOST HERE
user: "", // YOUR USERNAME HERE
password: "", // YOUR PASSWORD HERE
database: "datastore"
});
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
var urlParts = url.parse(req.url, true)
var key = urlParts.query.key
var value = urlParts.query.value
if (value == null) {
con.query("SELECT * FROM `store` WHERE `dataKey` = '" + key + "';", function (err, result, fields) {
if (err) {res.end("err")};
if (JSON.stringify(result[0]) == null) {
res.end("error");
}else{
res.end(JSON.stringify(result[0]));
}
});
} else {
con.query("SELECT * FROM `store` WHERE `dataKey` = '" + key + "';", function (err, result, fields) {
if (err) {res.end("err1")};
if (result[0]==null) { // MAKE ROW
con.query("INSERT INTO `store` (`dataKey`, `value`) VALUES ('"+key+"', '"+value+"');", function (err2, result2, fields) {
res.end("successful");
});
} else { // UPDATE EXISTING
con.query("UPDATE `store` SET `value` = '"+value+"' WHERE `store`.`dataKey` = '"+key+"';", function (err2, result2, fields) {
res.end("successful");
});
}
});
}
});
server.listen(port, hostname, () => {
console.log("Loaded. Running on "+hostname+":"+port);
});
Now exit and save the file and run it.
To run it do
node index.js
Now that the server is running we can now move to the Roblox side.
Get this model, and put it into studio.
Now open up the modulescript and put in the host. This must include the http://
and port or it will not work.
Here is an example:
local nodeDS = require(game.ServerScriptService.nodeDatastore);
local http = game:GetService("HTTPService");
local tablee = {Test="lol",Points=10};
nodeDS:Set("testKey",http:JSONEncode(tablee));
print(nodeDS:Get("testKey"));
VoilĂ , you now have set up your own custom datastore server!
Feedback would be appreciated!