All I want to do is install MongoDB Community Version on our Debian VM in Google Cloud Platform and insert any element in the database. (Don't want to use the preinstalled packages/ programs for sale on Google Cloud Platform).
I have been stuck on trying just to insert anything into mongodb. I dont know if it I installed something incorrectly or if my code is off (or if Google has restriction). I don't know what the problem is. If someone is kind enough to help out, I would really really appreciate it.
It is really long. Please if you have time, please help...
I followed the MongoDB installation first from:
Install MongoDB Community Edition - Debian
Here is what I did, from beginning to end.
1:) Created a VM Debian 8.0 Machine on Google Compute Engine (Installing MongoDB and NodeJS)
Make Google Cloud Work:
Putty Key Generator
- Generate
- Move Mouse
- Key Comment is Username
- Key Passphrase is Password
- Save Public & Private Key
Google Cloud Add SSH Key
- SSH Keys
- Edit
- Copy Paste Putty Key Code
Google Cloud VM Instance
- VM Instance
- Create Instance
- CentOS
- SSH
- View gcloud command
- Run in Cloud Shell
Linux Code
- cd /
- sudo su
- sudo apt-get update
- sudo apt-get install apache2
- sudo apt install php5
- cd /var/www/
- chmod -R 0777 html/
- cd /etc/
- chmod -R 0777 apache2/
PHPStorm
- Tools > Deployment > Configuration...
- STFP host: {IP ADDRESS}
- Root Path: /var/www/html
- Username: admin
- Key Passphrase: password
- {Remove index.html}
- {Upload project}
Allow .htaccess
- Run command: a2enmod rewrite
- Change file /etc/apache2/apache2.conf
- Replace in "AllowOverride None" to "AllowOverride All"
- service apache2 restart
Install NodeJS
- cd /var/www/html/backend
- Go to website "https://github.com/nodesource/distributions
- curl -sL https://deb.nodesource.com/setup_9.x | bash -
- apt-get install -y nodejs
- node -v
- sudo apt-get update
- sudo apt-get install build-essential libssl-dev
- npm -v
Install Packages
- cd /var/www/html/backend
- {Have package.json already}
- npm install express --save
- npm install cors --save
- npm install body-parser --save
- npm install curl --save
- npm install -g nodemon --save
Run NodeJS forever
- cd /var/www/html/backend
- npm install -g forever
- sudo chown root /etc/rc.local
- sudo chmod 755 /etc/rc.local
- chmod -R 0777 /etc/rc.local
- chmod -R 0777 /etc/init.d/rc.local
Edit rc.local file Before exit 0, add:
sudo service mongod start
cd /var/www/html/backend
forever start -c nodemon server.js
Upload file to /etc and /etc/init.d
- forever start -c nodemon server.js
Install MongoDB
- cd /
- sudo apt-get install git
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
- echo "deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
- sudo apt-get update
- sudo apt-get install -y mongodb-org
- sudo service mongod start
- service mongod status
- cd /var/www/html/backend
- npm install mongodb --save
Opened Port 8080 and 27017
============================================
2): The Relevant Code
So it seems that Mongodb is running and is connected. But when I want to insert data, the connection is disconnected on NodeJS. I dont know why. Here is the important relevant code:
On click, send data via AJAX to Node JS
function loginAdmin(){
//Get elements
const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const btnLogin = document.getElementById('btnLogin');
//When all input in fields are made
console.log("clicked");
$.ajax({
url: ipaddress.concat(':8080'),
type: "POST",
data: {
txtEmail: txtEmail.value,
txtPassword: txtPassword.value
},
success:function(data){
console.log(data);
return false;
}
});
return false;
}
Insert Data to MongoDB
router.post('/', function(request, response) {
var txtEmail = request.body.txtEmail;
var txtPassword = request.body.txtPassword;
mongo.connect("mongodb://"+ ipaddress +":27017/test", function(err, db) {
db.collection('user-data').insertOne("223");
txtEmail = "mongodb connecte22";
response.send(txtEmail);
response.end();
});
});
Success that connection with MongoDB:
Error when attempting to insert into MongoDB: