Difference between request and query? - javascript

I'm learning a bit of Javascript and mongodb , actually i installed ExpressJS which have .request , and QueryStringParser modulles which have .query . Which is the difference between both?

You don't say which QueryStringParser modules did you installed, but since you already mention MongoDB I assume you are talking about this https://github.com/diegohaz/querymen
Expres request handles the HTTP requests while Querymen's query is an Express middleware to build a MongoDB query from parameters in the HTTP querystring request nothing else.
You can read more about both in the following links
https://expressjs.com/en/api.html#req
https://github.com/diegohaz/querymen

Related

integrate sentry with parse server

I’m trying to integrate sentry with my parse server which runs express server behind the scene. I’m not being able to send my parse server cloud function errors to sentry. Note that, we don’t need to write any error middlewares manually. Parse does it for us.
app.use('/parse', api)
app.use(Sentry.Handlers.errorHandler())
there is Parse server option enableExpressErrorHandler, that does exactly what is required there - it pass errors to next middleware.
Just use enableExpressErrorHandler: true in your parseServerConfiguration dictionary.

Mongodb wrong return from Mycollection.find().count();

i have a problem with my app.
When written this db.request.count(); on mongo shell i have 4, but when i written Request.find().count() in my js code i have 0.
Someone can help me ?
Try using Request.count() instead of calling count() after find().
This is assuming the name of your model is Request.
The Mongoose documentation has examples here.
If you ran Request.find().count() on the client and got 0, it's most likely that your client-side code did not subscribe to the Request collection.
If you ran Request.find().count() on the server and got 0, make sure you connected to the correct database. I.e. Meteor creates its own Mongo instance on port 3001, whilst you may have been using a different database on 27017.

AngularJS - Interaction with mongodb

I am new to MEAN stack, I am trying to create a basic one page application at the moment.
I am trying to connect to the mongodb and then list the values in a certain collection in a controller.
However, when I looked for the answer, I came across this answer
Using AngularJs and MongoDB/Mongoose
Which then confuses me as what is the point of having the code below if you can't use it between angular and mongo ? Or are there other interim steps that use it?
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost:3000/database');
var orderSchema = new mongoose.Schema({
routeFrom : String,
routeTo : String,
leaving: String
});
var Order = db.model('Order', orderSchema);
module.exports = Order;
Edit: The situation i am trying to use it in is such:
Geek.html
<div class="jumbotron text-center">
<h1>Geek City</h1>
<p>{{tagline}}</p>
<ul>
<li ng-repeat="value in dataValues">
{{value.name}}
</li>
</ul>
</div>
GeekController
angular.module('GeekCtrl', []).controller('GeekController', function($scope) {
$scope.tagline = 'The square root of life is pi!';
$scope.dataValues = function(){
var mongo = require('../config/db.js');
var collectionValues = mongo.myCollection.find();
return collectionValues;
};
});
You cannot require db.js config file in Angular because it's not set to be used on the client side. What you describe is so called 'Isomorphic' approach.
What I mean by that: mongo is a database system (roughly speaking). To get data from the database, we usually don't trust the client. So we have some server-side code (PHP, Ruby, Node.js, Java, what have you) which authorizes the client, processes and filters the data and returns it to the client (in this case Angular.js). So your Mongoose models are set to be used by the server-side javascript and that part of the app. That server side should also serve data to Angular so you'd connect to Node.js from Angular, not directly to Mongo. So the same server that (usually) serves your angular files, will also serve the data it reads from mongo.
If you want server-less data with Angular, you can take a look at Firebase.js. It's angular-ready and it could help you not mess around with Mongo, mongoose and the server-side code.
You could try a hybrid approach with something like meteor.js or backbone.js set to work both on client and server, or take a look at this article for more info.
Or for what it's worth, if you want to run your own Mongo, you could start mongo with --rest, then you'd be able to connect to Angular directly to Mongo, at http://somehost:28017/mydatabase or something similar, depending on your setup.
Mongoose is a node module, and as far as I know it doesn't have a front end component, so you won't be using it directly in your frontend js code. It's only going to help you on the server side of your app. If you're relatively new to Node then this stuff can get pretty confusing, since it's all end-to-end javascript and sometimes it's not clear what modules work on the server or frontend, especially since some can do both.
Node, MongoDB, Express, and Mongoose all live on the server.
Angular lives in the browser, and can't use any of the server-side components directly.
Using the MEAN stack, You will be building a node app that uses mongoose to talk to mongodb and express to expose an api to your front end. Then in in your html/js code you'll be using angular and its $http service to talk to the server to get and set data.
There is a great tutorial that walks you through the entire process on scotch.io:
http://scotch.io/bar-talk/setting-up-a-mean-stack-single-page-application

Is there an equivalent of Netscape navigator functions in nodejs?

Can I access the inbuilt navigator functions like isinNet() or DomainNameorHost() from nodejs?
Since nodeJS runs on the server, not the browser, you can't access functions that are only provided in a browser.
Most developers use a middleware like Express to create a web service on nodejs.
In a route, such as
app.route("/play", function(req,res){
// code that handles URL /play
});
there is a callback function that is called when a request arrives for that route.
The req object parameter contains everything about the request.
req.ip is the upstream (incoming) ip address.
I looked around in npm for a module that might map remote ips to hostnames and could not find one. Presumably all it would do is reverseDNS, which could take time and hold up processing requests.

node.js how to respond .js files with parameters

I have a node.js http application and i'm servicing GET requests ok. I can't however respond properly with requests for say foo.js?_param=1234. How do i deal correctly with files of this type where parameters are being passed?
EDIT:
I'm using express to service files as follows:
app.get('/*', function(req, res) {
res.sendfile(__application+req.url, {root: __root});
});
__root is the root path of the application.
Use request.url, it will look like /foo.js?_param=123.
Then use require('url').parse(url,true) to split this into meaningful parts (true is to also expand individual query string parameters).
See http://nodejs.org/api/http.html#http_request_url for details.
Try using the express module.
They have a whole API for dealing with GET and POST requests.
You can use req.query to handle the get requests.

Categories

Resources