Express Get api - angularjs

I'm practicing right now with the MEAN stack.
I've made a project with the Angular shell and I've inclueded express in my project.
Here's my first GET, i want to use it to retrive some data from my mongoDB, in this case an entire collection:
router.get('/biscottigrazie', function(req, res) {
var db = req.db;
var collection = db.get('biscotti');
});
Maybe I'm missing the concept but i think that from angular i should be able to "call" this GET and get the data.
So, my var collection should contain the data, how can I retrive them from angular files?

You will not get db instance from request variable. You will get it while you define database.
var db = new Db('test', new Server('localhost', 27017));
router.get('/biscottigrazie', function(req, res) {
var collection = db.getCollection('biscotti');
res.send(collection);
});

Related

REST API Confusion

I'm trying to write a simple rest API to connect to my sql Server database and execute a simple query to retrieve data from a database.
I'm following this tutorial: https://medium.com/voobans-tech-stories/how-to-quickly-create-a-simple-rest-api-for-sql-server-database-7ddb595f751a
Here's where my confusion lies:
The example has a server initialization file that looks like this:
var express = require('express'); // Web Framework
var app = express();
var sql = require('mssql'); // MS Sql Server client
// Connection string parameters.
var sqlConfig = {
user: 'UserName',
password: 'mot de passe',
server: 'localhost',
database: 'DatabaseName'
}
// Start server and listen on http://localhost:8081/
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("app listening at http://%s:%s", host, port)
});
It also has a select query the example uses on the customer table located in the database:
app.get('/customers', function (req, res) {
sql.connect(sqlConfig, function() {
var request = new sql.Request();
request.query('select * from Sales.Customer', function(err, recordset) {
if(err) console.log(err);
res.end(JSON.stringify(recordset)); // Result in JSON format
});
});
})
What I'm not understanding is how these two files are connected or interacting. At the end of the tutorial, the author says in order to test the example, copy the code into a file and run it. The example has 4 separate files though - am I putting them all into the same document?
yes you can put all code snippets in the same file for your test. It will be the easiest way since they all using the app variable.
But if you want a bigger application, it's cleaner in multiple files. You can then use code from another file using an import like require('./filename.js');

Calling nodeJS from angularJS function

I have one function which block one room from room list via ajax call here is function
$scope.blockThisRoom = function(room, index){
// ajax call an validation
}
I have setup nodeJs script
var http = require('http');
var url = require('url');
var fs = require('fs');
var server;
var io = require('socket.io').listen(server);
server = http.createServer(function(req, res){
var queryObject = url.parse(req.url,true).query;
console.log(queryObject)
}),
and it gives query object while I am doing testing.
My Problem
I want to call nodeJS script when $scope.blockThisRoom function is call but I am confuse how to call node function.... , should I include nodeJS script as we add other javascript files in my view
or
I call nodeJS file a via ajax calling like below is code
$scope.testController = function() {
var url = "http://localhost:8076?id=1&type=block";
$http.get(url).success( function(response) {
$scope.students = response;
});
}
Angular and node are differents : NodeJs is executed in server, and angular in client. So If you try to include node script and run it on client browser, It will just not work... The two are javascripts, but not linked by any ways: you can use angular on PHP projects, and you can use Node.js without angular. It is just the fact that the two technologies are powerful when used together...
Effectively, I suggest to use Ajax call, with parameters you want (use $http.post instead of $http.get if you want to send a long query, or if you want to send arrays or objects... because with node and Angular post, you can send/parse JSON strings!!)

Firebase syncObject is showing redundant error in my Angular controller

I am building an app for a client who has, as one of their data sets a master list of all their members. I have the data coming in from Firebase and everything runs peachy, but it's not that DRY I am now realizing. I would like to use some of the data from the membership set in other views within the site. I copied the code listed below into other controllers that I need to have access to it and although everything works, my IDE (RubyMine) shows the 'syncObject' as redundant.
So, my question is, if there's a way to code it better and dryer to be used in other views? Thank you for your time.
.controller( 'MembershipCtrl', function MembershipCtrl( $scope, $firebase ) {
var ref = new Firebase("https://myid.firebaseio.com/members");
var sync = $firebase(ref);
var syncObject = sync.$asArray();
$scope.members = syncObject;
});

How to Connect to postgres db from angularjs controller

I want to store some user values, name and email, in a Postgres DB on an app hosted by Heroku. This is the code in my controller;
var pg = require('pg');
...
$scope.addUser = function() {
pg.connect(process.env.DATABASE_URL, function(err, client) {
var query = client.query('INSERT INTO users (name, email) VALUES (\'' + $scope.user.name + '\', \'' + $scope.user.email + '\')');
query.on('row', function(row) {
console.log(JSON.stringify(row));
});
});
};
I get an error saying that require is not defined, which I know is because it is not supported client side. I tried using browserify on the controller file, then updating the <script> tag in the index.html that sources the file. This leads to another error.
Removing the var pg = require('pg'); gives a "ReferenceError: pg is not defined". I just want to connect to the Postgres DB from my app and insert some values. Am I on the right track or should I be going about this differently?
See Can I use PostgreSQL (pg) in the client-side (express/node.js) as to why you can't let clients connect directly to your database. You either want to write a (lightweight) server app (probably using Node.js), or you should consider a backend-as-a-service (BaaS) like Firebase or IrisCouch so that you don't need to develop the backend at all.

Backfire (Backbone-Firebase) Collection not Loaded

So basically, I created a Backfire app. As soon as the page is loaded, I am iterating over my Backbone.Firebase.Collection instance.
And then rendering the results onto the page... the problem is that obviously the collection instance does not contain all the data that is on firebase.
So, I put a set-timeout of 1000ms to make it so that the collection has time get its data from Firebase. This way of doing it is terrible... sometimes the data is synced up other times it is not.
How can I have an event be triggered as soon as the "BackFire Collection Model" has its data from firebase?
$(document).ready(function() {
var myCatalogApp = new CatalogApp()
var catalog = new Catalog()
setTimeout(function(){myCatalogApp.displayCollection(catalog)},1000);
//To give catalog time to asynchronously retrieve database entries
myCatalogApp.initialize()
})
var Catalog = Backbone.Firebase.Collection.extend({
model: Item,
firebase: "https://mycatalogmaker.firebaseIO.com/items"
});
All of my problems would be solved if Backfire would let me do a fetch method... but they have it disabled for Backfire objects
Try this :
$(document).ready(function() {
var myCatalogApp = new CatalogApp()
var catalog = new Catalog();
catalog.bind('sync', function(){myCatalogApp.displayCollection(catalog);});
myCatalogApp.initialize();
})

Resources