Heroku Postgres Connecting in Node.js - node.js

I want to use a heroku database (already deployed in heroku) for my AngularJS application created using Yeoman. I want to persist my data that is currently an array of JSON objects that goes away when I refresh the page. I am trying to follow Heroku's guide for Node.js here (https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-node-js) but it is very shot, no examples, and I am fairly new to servers/databases. I have a 'web.js' file and the Procfile int my root directories for Node.js and heroku to read that file. I have the "dependencies" already set but I am not sure what is happening in this code below that heroku provides
var pg = require('pg');
pg.connect(process.env.DATABASE_URL, function(err, client) {
var query = client.query('SELECT * FROM your_table');
query.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
First: Where do I put this code?
Second: What is happening here?
and Third: How do I use it to upload my data that is currently an array of JSON objects that I
hardcode into my code into the heroku database?
My web.js file
var gzippo = require('gzippo');
var express = require('express');
var app = express();
app.use(express.logger('dev'));
app.use(gzippo.staticGzip("" + MyApp + "/dist"));
app.listen(process.env.PORT || 9000);
My Procfile
web: node web.js

That code goes in your web.js file.
I'm pretty sure it's accessing your database and setting the results to the variable query so you can access the data.
I think want you want for pushing the data is to look here, particularly at pg:push.

Related

Best Practice to create REST APIs using node.js

I am from .Net and C# background and I am new to Node.js. I am working on a project, which is mix of MongoDB and Node.JS.
In MongoDB, data from various tools is stored in different different collections. I have to create multiple REST APIs using Node.JS for CRUD operation on that data, these APIs will be called from React.JS application.
I want to keep APIs into separate files for seperate tool and then calling including all files into app.js file.
Please help me with best approach.
For POC purpose, I created a node.js application, where I created app.js file and written all my code for GET|POST|DELETE APIs. This is working fine.
var _expressPackage = require("express");
var _bodyParserPackage = require("body-parser");
var _sqlPackage = require("mssql");
var app = _expressPackage();
var cors = require("cors");
var auth = require('basic-auth');
var fs = require('fs');
const nodeMailer = require('nodemailer');
//Lets set up our local server now.
var server = app.listen(process.env.PORT || 4000, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
app.get("/StudentList", function(_req ,_res){
console.log("Inside StudentList");
var Sqlquery = "select * from tbl_Host where HostId='1'";
GetQueryToExecuteInDatabase(_req,_res, Sqlquery,function(err,data){
console.log(data);
});
});
Don't know exactly what your app intends to do, but usually if you are not serving webpages and your API is not too complex, there is no need to use express. You can build a simple server natively in NodeJS to serve data.
Additionally, if your app has many routes (or is likely to in the future), it is a good idea to put helper functions like GetQueryToExecuteInDatabase() in a separate file outside of app.js such as utils.js.
Based on what I have understood about what you want to do, your file structure should look something like this:
data (db related files)
services (contains one file per api service)
app.js
utils.js
Hope this helps.

App using socket.io and Node.js works locally but not deployed to heroku

Tried other solutions I could find such as adding process.env.PORT and also looked at heroku's guide on socket.io.
The Phaser.io game my group is making works perfectly locally, however when we deploy to heroku there is a bug where only the first player connected will see the other players, and only for a brief moment as well. After that short period, the players freeze.
My question is, does anyone have any clue what could potentially be causing the issue? Is it something wrong with our implementation of socket.io?
Thank you in advance for your help. Extremely confused as to what is causing this issue.
Here is a deployed version: https://death-road-to-toronto.herokuapp.com/
Here is the repo if you'd like: https://github.com/Road-To-Capstone/CapstoneMultiplayer/
Here are the code snippets relevant to socket and express
Index.js
const config = require('./config');
const express = require('express');
const socketio = require('socket.io');
const http = require('http');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
app.use('/', express.static(config.publicDir));
//-socket
const players = require('./players.js');
const missiles = require('./missiles.js');
const zombies = require('./zombies.js');
io.on('connection', socket => {
-------
});
//=socket
server.listen(process.env.PORT || config.port, () => {
console.log(`Listening on ${config.port}`);
});
//creating new zombie id
function newZombieId() {
let id = new Date();
return id.getTime();
}
config.js
const path = require('path');
module.exports = {
port: 3000,
publicDir: path.join(__dirname, '../public'),
game: {
start: {
x: 400,
y: 400
}
}
}
EDIT1 : I made a funny discovery while playing around with socket.io and heroku. So if I connect with another computer to the deployed version on heroku, then click on console (just to freeze the game). Anyone at that point can join and everything works perfectly, just like in the local copy.
Does anyone know why? If not what can I do as a work around? Perhaps implement a lobby system and allow all players to join and start at the same time?
Solved my problem! Going to leave my question and the solution here for prosperity, incase other people want to implement a multiplayer game using Phaser and socket.io on Heroku and also run into issues.
We added this to the index.html file </script type ="text/javascript" src="/socket.io/socket.io.js"> </script> which allows heroku to access the socket library.
It actually wasn't a setup issue with socket.io, it was our logic. When a player detects movement it instantly tries to socket.broadcast that a player moved. However, when other players first connect, they don't have time to retrieve all the players information from the server side. Thus the broadcast would cause an error where "player" is undefined. Adding a conditional statement fixed our issue.
The code in it's entirety is going to be left on github if you want to investigate further.

Referencing NodeJS MongoDB connection from AngularJS

I'm trying to set up a small development environment consisting of a node.js http server, a mongodb database and frontend in angular.js. For development purposes I've created an account with MongoHQ and can reference my db using a URL.
The issue I'm faced with is that I can easily connect to my db from my Angular code but then my connection info is exposed through my http server.
So what I would like to be able to is to create my connection in my NodeJS server.js file and reference it from eg. my AngularJS app.js file. Is that possible and if so how?
Thanks in advance
Try using express and mongoose.
Server side code
var express = require('express');
var app = express();
//start server with port of choice here
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Foo=mongoose.model('foo');
//foo is a model. Check mongoose documentation from collections and schemas
app.get('/hello', function(req, res){
//get data from mongo using mongoose
foo.find({},function(err,docs){
//do stuff here
res.send(docs)
})
});
Front end code
$http.get('/hello').success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
//data is the data from mongodb
})
Follow this tutorial to get an idea
There is a stack called MEAN Stack. See if it fits your needs. Very easy to set up and develop.

How can I structure my express app where I only need to open a mongodb connection once?

Note: Please read the edited portion of this post before answering, it might save you time and answers one of my questions.
The problem I'm having is pretty simple but I'm pretty new to this overall and I'm having issues figuring out how to implement a mongodb database connection properly in a node/express app.
I'm using express 3.x and am basing my app layout around this project supplied by the author of Express:
https://github.com/expressjs/express/tree/d8caf209e38a214cb90b11ed59fd15b717b3f9bc/examples/blog (now removed from repo)
I have no interest in making a blog however the way the app is structured appears to be quite nice. The routes are separated and everything is organized nicely.
My problem is I might have 5-6 different route js files and each route js file might have anywhere between 1 and 15 routes; of those routes 1 or 15 might want to access the db.
So my problem is it seems like a really terrible idea to do a db.open(...) every single time I want to query the db. I should mention at this point I'm using the native mongo-db driver (npm install mongodb).
I would also need to include a file like this:
http://pastebin.com/VzFsPyax
...in all of those route files and all of my model files. Then I'm also dealing with dozens upon dozens of open connections.
Is there a way I can structure my app in such a way where I only make 1 connection and it stays open for the duration of the session (having a new one made every request would be bad too)?
If so, how can I do this? If you know the answer please post a code sample using tj's blog app (the one linked earlier in this post) structure as a base guide. Basically have a way where the routes and models can use the db freely while being in separate files than the db open code.
Thanks.
EDIT
I made some progress on solving one of my issues. If you look at tj's blog example he initializes his routes in the app.js like so:
require('./routes/site')(app);
require('./routes/post')(app);
And in the routes js file it starts like this:
module.exports = function(app){
I stumbled on a project earlier today where I saw someone pass 2 variables in the modules.exports call -> function(app, db). Then figured wow could it be that easy, do I need to just adjust my routes to be (app, db) too? Yeah, it seems so.
So now part 1 of the problem is solved. I don't have to require a mongo.js file with the connection boilerplate in every route file. At the same time it's flexible enough where I can decide to pick and choose which route files pass a db reference. This is standard and has no downside right?
Part 2 of the problem (the important one unfortunately) still exists though.
How can I bypass having to do a db.open(...) around every query I make and ideally only make a connection once per session?
Other solution is to pass database to the router via request, like this:
app.js
var db = openDatabase();
var app = express();
app.all('*', function(request, response, next)
{
request.database = db;
next();
});
app.get('/api/user/:id', Users.getByID);
users.js
var Users =
{
getByID: function(request, response)
{
request.database.collection('users').findOne(...)
response.send(user);
}
};
module.exports = Users;
I made a very simple module hub for this case that replaces the use of a global space.
In app.js you can create db connection once:
var hub = require('hub');
hub.db = new Db('foobar', new Server('10.0.2.15', 27017, {}), {native_parser: false});
And use it from any other files:
var hub = require('hub');
// hub.db - here link to db connection
This method uses a feature of 'require'. Module is only loaded for the first time and all the other calls gets a reference to an already loaded instance.
UPDATE
That's what I mean:
In main file like app.js we create Db connection, open it and store into hub:
app.js:
var hub = require('hub');
hub.mongodb = require('mongodb');
hub.mongodbClient = new hub.mongodb.Db('foobar', new hub.mongodb.Server('10.0.2.15', 27017, {}), {native_parser: false});
hub.mongodbClient.open(function(error) {
console.log('opened');
});
Now in any other file (message for example) we have access to opened connection and can simple use it:
message.js:
var hub = require('hub');
var collection = new hub.mongodb.Collection(hub.mongodbClient, 'message');
module.exports.count = function(cb) {
collection.count({}, function(err, count) {
cb(err, count);
});
};
Really silly. In the documentation it seems like db.open requires to be wrapped around whatever is using it, but in reality you can use it without a callback.
So the answer is to just do a db.open() in your database connection module, app.js file or where ever you decide to setup your db server/connection.
As long as you pass a reference to the db in the files using it, you'll have access to an "opened" db connection ready to be queried.

How to serve client-js from push-it using node.js

I have just started using node.js. My mayor problem is lack of documentation but I'm getting through and I really like it
Now I'm trying to use push-it which sits on top of socket.io. The Docs mention to serve the static client-js file, but I don't know how to do that. I already tried different paths. Socket.io works out of the box, but I can't find how to do it for push-it.
I installed push-it using npm
Thanks for any tips,
Miguel
you can use connect or express to server static files,
exactly as the dnode docs suggest.
__dirname is the directory you're running from, it's common to use __dirname + '/public' and place your files in there
var connect = require('connect');
var server = connect.createServer();
server.use(connect.staticProvider(__dirname));
var dnode = require('dnode');
dnode(function (client) {
this.cat = function (cb) {
cb('meow');
};
}).listen(server);

Resources