Node.js + Sails.js + MongoDB [Part 2]: Configuring MongoDB

In this post I’ll be picking up from where we left off in the last Sails.js with MongoDB post. Last time we saw how to install and configure Node.js and MongoDB on Ubuntu, as well as using the Node Package Manager (NPM) to download and install Sails.js – a realtime MVC web framework that runs on Node.js.

Here’s what we’ll check out today:

  • Configure MongoDB and add an admin user
  • Create a simple database and collections

Configure MongoDB and add an admin user

Before starting to create any databases, we need to create a new administrative user for our MongoDB instance. This is done so we have something like a superadmin that can create other databases, create new DB users, and assign those users as database administrators.

First make sure the mongod service is running, by “sudo service mongod start“. Then we use “mongo” to get into the MongoDB environment. Prior to running the following, we don’t need to be authenticated if we’re physically on the machine that MongoDB is installed on, by using the localhost exception. This is only used for configuring MongoDB for first use, so make sure you remember the admin password you will set as the localhost exception is wiped after the following.

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

 

We now have the admin role for creating databases and assigning users to those databases. Now let's create a new database and assign a new admin user to it. In MongoDB a database is created only if we insert some stuff in, but we can go ahead and use a database that doesn't exist, in order to perform some actions and prepare it before inserting content. To do this, we actually perform the same steps as we did above for adding the admin user.

use test

Now that we've selected the database that is to be created, let's add a user to it:

db.addUser({ user:"rwUser", pwd:"password", roles:["readWrite"] })

 

Create a collection

We can now go ahead and create tables. Actually, in MongoDB tables are called collections. So let's create a collection and add some stuff in it.

db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )

 

We've now created a collection named "log" and defined some properties for the collection, such as the max size and the maximum amounds of records (documents) that we can put in the collection.

Next time we'll see how a Sails.js app can insert and manipulate documents in a MongoDB database.

Neo&Bee: Another one bites the dust

It is now considered definitive that Mr. Neo will not resurface. After pumping and dumping the virtual market with fear uncertainty and doubt, Neo is now a bad memory in the short history of Bitcoin and cryptocurrencies. However, this should not affect Bitcoin. It really isn’t related to Bitcoin, it’s related to a single person who decided to defraud hundreds of [virtual] investors. Bitcoin hasn’t failed as a system, and this needs to be understood, especially by Cypriots. Neo wasn’t Bitcoin. Neo was someone who exploited a situation. People have been commiting fraud using euros and dollars and any other currency you can think of, since the beginning of money. Just because people are now doing it using Bitcoin, or any cryptocurrency for that matter, doesn’t make them any less criminal as it also doesn’t matter what monetary system they used to commit fraud.

I’m not saying that Neo & Bee, the company, committed fraud. I’m saying the man behind the company may have committed fraud. In the beginning it all looked like Cyprus just wasn’t ready for the concept and the company was failing from its very start due to being incapable of generating a customer-base. It seems though, that everything was looking mostly positive despite the ups and downs and different announcements from the Cyprus central bank and the conversations at the parliament. We now know of course, that it is likely that Danny Brewster, Neo’s CEO, still has control of many of the initial investments as well as owes a lot of money to several creditors in Cyprus. It’s possible that much of the company’s funds were stuck in Bitfunder, which may be what initially led to its demise; however, the unexpected and unexplained absence of DB as well as the fact that he hasn’t communicated with anyone apart from a supposed comment he made to CoinDesk, point to the fact that even if the initial concept wasn’t to defraud people, that’s definitely what has happened.

It has been spread through rumors that Danny Brewster wanted to open wallet accounts for customers, and that the rest of Neo employees pushed to delay until the company was more ready. So, if the company  wasn’t ready for business, then why did we see Neo & Bee advertisements everywhere? From TV to the radio, billboards and magazine ads, Neo was seemingly a big thing. They even went as far as moving into fancy offices and hiring quite a few employees. They genuinely intrigued many people to find out more about it through their website or by calling them on the phone, however the ones who asked to open an account were temporarily dismissed with general information leaflets about Bitcoin, presented with the company’s (poor, if you ask me) prospectus and a promise that they would call them back as soon as they were ready t0 open accounts for them.

In this sense, it is quite fortunate that they had not opened up accounts and did not take money from the general public. Of course there are allegations of two people paying sums of up to 15 and 20 thousand for bitcoin which they eventually never received, but this is still under investigation and we don’t know if that is true. It wouldn’t surprise me if it was though, due to the way everything turned to dust in a matter of days. The big shame of this whole ordeal, is of course the creditors of the company. Not even the investors, since investing is never guarranteed to work out. But the creditors, such as advertisers and people who provided other kinds of services to Neo & Bee are the ones who are in deep due to them. I have heard of 2 Cypriot companies shutting down due to Neo and there is probably a third that will most surely shut down due to overwhelming debts owed to them by Neo.

It is unfortunate that all this has happened. However, myself and other cryptocurrency enthusiasts that I speak to in Cyprus are determined to help lift the distrust of Bitcoin. Cryptographic digital currencies offer many advantages in contrast to fiat currency and the possibility should not yet be dismissed in Cyprus just because of what happened with Neo & Bee. What we all need to remember is that one of the true mottoes of any cryptocurrency is “be your own bank”. Therefore anyone who tell you they’re planning to be a “bitcoin bank” should be treated not with just a grain of salt but with a whole handful.

 

 

 

Node.js + Sails.js + MongoDB [Part 1]: Installation

1898672_674689092577202_520955318_o

 

MongoDB is really simple to get started with. It is especially easy to use if you’re using some sort of framework such as Sails, or MEAN.

First make sure MongoDB is installed on your system. In Ubuntu it’s pretty easy:

sudo apt-get install mongodb

Of course, if you're missing dependencies or the MongoDB repo, you'll have to add it. If this is the case for you, you will need to follow the longer tutorial found at http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Once MongoDB is installed, let's get Node.js. Again, if you're on Ubuntu, note that the package you need is "nodejs", not "node" which is a different package. If you do install the "node" package (which is the Amateur Packet Radio Node), it will create complications in starting Node.js modules in the future as you will be forced to use the command "nodejs" instead of "node". If this happens, or if the node package was already installed, it should be removed if not needed so it doesn't conflict with Node.js. If you decide to remove this package, by running

sudo apt-get remove node

sudo apt-get install nodejs

you may notice that the command "node" may still not work in your terminal, even after restarting your terminal you may get something like

-bash: command node not found

To rehash your environment variables in Ubuntu, run the command hash -r. I believe that in other distros this might be simply "rehash" but I'm not all too sure about this, so try both "man hash" and "man rehash" before you do this. After rehashing, the node command should work fine and Node.js modules should also work by just calling the name of the module (i.e. mean, sails, grunt).

Now, if you've gotten this far, the easiest thing to do, learning-wise, is to install Sails.js and start building small MVC apps to get the hang of it. Even if you don't know much of Node.js programming, if you know Javascript then Sails.js will help you not only learn Node.js but also build scalable, data driven, realtime apps with ease.

To install Sails.js we run

sudo npm install -g sails

Now, NPM (Node Package Manager), should have been installed along with Node, if you've successfully installed Node.js latest version. (we can find Node.js version by running node --version). If npm is not found, you may have an older version of Node.js - this happens often with Ubuntu users if they haven't ran sudo apt-get update in a long time or after updating their repositories. To make sure you have the latest Node.js repo included you should search Chris Lea's Node.js repository and include that through PPA. In any case however, NPM can also be installed separately if a specific older Node.js version is needed. Also, in case you need to interchange between Node.js versions for maintaining older projects,  try installing the Node Version Manager (NVM).

In the above command which install Sails.js, we need to take notice of two things. In Ubuntu, and also in most other distributions, you'll need to run NPM under sudo. If you're running this in Windows then no special superuser command is needed. If you're developing on server 2008 R2 for some reason though, you may need to start cmd.exe or any terminal you're running, by right clicking on it and selecting "Run as administrator". The other thing we need to check out in the command above, is the -g option. This is the "global" option, which tells NPM to install Sails.js (or any package specified), in the global directory of Node.js. Without this option, Sails.js will be installed in the local directory and will only be accessible in the project you're currently working on.

Through this simplistic tutorial we've taken care of installing our MongoDB database, Node.js, NPM, NVM, and Sails.js. In upcoming posts I shall go over the following:

  • Configure MongoDB and add an admin user
  • Create a simple database and collections
  • Installing the sails-mongo module and configuring our Sails adapters.js to use our MongoDB database
  • Creating a small application with Sails

* It's always wise to have python, g++, make and build-essential installed in your system prior to starting any of the above. If you don't you may run into dependency issues, which can be solved, but the headache can be avoided if these dependencies already exist.

If you have any questions I'd be glad to help you out. Hit me using the form below.

Node.js and Sails

So lately I’ve been pretty consumed with the events in the cryptocurrency space, which in combination with my master’s dissertation writing it naturally has caused me to leave my coding and learning a bit behind.

My goal, after finishing all my graduate work at the university, is to commit to becoming a skillful Node developer. I am completely sick of PHP. Even my latest posts where I might have sounded like I was defending it, if you go back and read it all I was saying is that PHP can be good and secure if the coder is good. However, let’s think about this for a second. When I write in PHP, I find it increasingly hard to stick to a strict set of principles. I’m all over the place. I’ve been using CodeIgniter because I’m fond of the MVC paradigm, however CI has so many problems which need to be fixed in each and every single project. I’m not ranting here, it’s just the way it is.  Also, no matter the framework, working with HTTP in PHP feels like you are probing the server with a stick and waiting when it’ll wake up in order to get a response.

In Node all that is different. Node was built with HTTP/1.1 in mind and that’s really where it excels. I remember last year watching a presentation on Node.js, where this guy had a small blogging system with a backend for registration, login and posting in about 35 minutes. So, I thought to myself, this thing scales really well and it seems to be really easy to learn – given that you know JavaScript and understand HTTP. But that just got me intrigued. What really lit the lamp for me though, is a speech/workshop by Ryan Dahl (Node.js creator), where he gave his IP address to the audience, wrote a 10-line script which the audience copied in their own node terminals, and within a matter of seconds you could see everyone’s typed words on Ryan’s terminal running one of the nodes. It was a chat script. In 10 lines, in 5 minutes. What made it really amazing for me is that you could write this script with either HTTP or TCP with sockets. It didn’t matter, Node takes HTTP requests in real time, and at the same time from all the terminals and processes them in parallel.

I’ll now be learning to node and Sails.js makes it much easier to create and learn at the same time as it is something I am used to. It works as an MVC framework, so it’s really easy for me to piece things together. So, if you’re wondering what my blog’s current octopus background is, you better pay sailsjs.org a visit and find out.

Why Cyprus is still behind when it comes to Bitcoin

Bitcoin has been making important headlines around 1.5-2 years after its birth, since 2011, yet here in Cyprus nobody bothered to really look up what was happening until much later. It wasn’t until the banks closed down on March 15th 2013 that suddenly Cyprus mattered in the bitcoin world. The people lost complete trust in the banking system and witnessed first hand that the government cannot help secure their funds. Rather, the feeling that if you have money in the bank you can lose it  to bail the bank whenever needed, is still lingering today and will be for many years to come.

Well, surprise-surprise, banks cannot be trusted. The government is corrupt, and if you’re asking me, it will always be so since that’s what governments are designed for, corruption. You know what would make a government more transparent? Cryptocurrencies can. See, the secret of having a central bank is that you can hide whatever funds in whichever way you see fit, and not get caught very easily. Do you think it is a coincidence that nobody knew how much money Cyprus was missing? One day you heard “we need 9.5 billion”, the next day you’d hear “we need 15 billion”. In the case of Mt.Gox though last month, you knew by blockchain analysis how many bitcoins were held by Gox addresses. bitcoin is not anonymous as most think. It provides transparency, but to be truly transparent and to be usable in government settings, we need regulation.

In Cyprus, Neo & Bee has been pushing for this kind of regulation. If bitcoin and other cryptocurrencies are regulated in a correct manner, then merchants will be less hesitant to adopt, and the government will possibly one day stop using the Euro and use bitcoin or any other cryptocurrency they see fit. They can even roll out their own crypto coin if they like. The significance of this is that no central bank is needed, trading parties do not need to trust each other and there is no need to rely on private banks or payment gateways to process money digitally. It is all done in the p2p network and the transactions are cryptographically undeniable. Furthermore, transactions are non-rechargeable, which allows merchants to feel safer when accepting money. Safer than Visa, Mastercard or PayPal, under which the user can attempt to cancel the transaction even if he has received the ordered product.

But what regulations can you have in a decentralized currency which is not controlled by any single entity? Well, what is needed is fundamental rules which will ensure to merchants and buyers alike that there doesn’t lie a future ahead where prosecution awaits them simply for using a cryptocurrency. The merchants need to know where and how to charge VAT and what to declare in their tax forms each year, none of which are clear when you work with bitcoin. For instance, and at least in Cyprus, the government, parliament and central bank refuse to acknowledge bitcoin as currency. The latest adopters of bitcoin in Cyprus were the hotel association. So if I stay at a hotel for 4 nights and I’m charged 1BTC for my stay, will I have to pay VAT? Will the hotel need to report in their tax forms that they got 1BTC from me? It is not clear and these questions bring fear to other merchants who may see the value of bitcoin but are reluctant to adopt due to lack of regulation.

In this regard Cyprus is still far behind regarding bitcoin adoption. Not only are there not many people who really know the technical side of bitcoin, therefore not allowing the increase of bitcoin startups, not that many are looking into the legal side of bitcoin. Cyprus stopped being a financial paradise last year’s March. However if we push forward for regulations, and also those of us who know a tiny bit about the technicalities of bitcoin such as security and development, teach others, we will be on a good way to a more euro-free and worry-less Cyprus. Cyprus is small and this is our chance to become world leaders in such an exciting field. Despite our small size we can make a difference, as long as we believe it and as long as we stop voting for monkeys.