Forum 1


Express is a node module. Used in web applications
Express is one of a web framework for web applications.
Nodemon : npm package for run the code.
When we start run nodejs application EventLoop is running behind node.js application. Waiting for some kind of asynchronous operations, connections being connected to the server

Node.js do IO non blocking, you can do a read write operation without blocking the sequential operation

https://medium.freecodecamp.org/what-exactly-is-node-js-ae36e97449f5
https://think360studio.com/12-benefits-of-using-node-js-for-web-application/
https://codeburst.io/all-about-http-in-node-js-and-3-best-ways-for-http-requests-in-web-development-6e5b6876c3a4
http://www.java2s.com/Tutorials/Javascript/Node.js_Tutorial/0130__Node.js_Loop_Statement.htm
https://medium.com/@tkssharma/node-js-interview-question-set-01-73c2b87c6ae3
https://medium.com/@tkssharma/node-js-interview-question-set-05-3df44d32d3a3

node Debugger :
https://www.tutorialsteacher.com/nodejs/node-inspector

Intor to Mongoose for MongoDB and Node.jshttps://code.tutsplus.com/articles/an-introduction-to-mongoose-for-mongodb-and-nodejs--cms-29527

Hashing passwords with NodeJS and MongoDB: bcrypt
https://solidgeargroup.com/hashing-passwords-nodejs-mongodb-bcrypt

Mongoose - Whats the difference between find({},cb) and find({}).exec(cb)?
Both execute the query and then run the callback.

The main difference is that the first one, returns a Query object while the second returns a Promise which is useful if you need promises.

const query = Model.find({}, cb);
Then you can work with the query variable.

While the promise...

const promise = Model.find({}).exec();
Then you can work with the promise and do things like:

promise.then(cb);
promise.catch((err) => {
console.error(err);
});
But if you do Model.find({}).exec(cb); the callback is also called without using promises...

I hope it helps
==========
Basically when using mongoose, documents can be retrieved using helpers. Every model method that accepts query conditions can be executed by means of a callback or the exec method.

callback:

User.findOne({ name: 'daniel' }, function (err, user) {
//
});
exec:

User
.findOne({ name: 'daniel' })
.exec(function (err, user) {
//
});
Therefore when you don't pass a callback you can build a query and eventually execute it.

You can find additional info in the mongoose docs.

UPDATE

Something to note when using Promises in combination with Mongoose async operations is that Mongoose queries are not Promises. Queries do return a thenable, but if you need a real Promise you should use the exec method. More information can be found here.
https://mongoosejs.com/docs/promises.html
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Node Error Handler :
-------------------

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
************** Alternate ****************
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});
=====================================
Node.js: Difference between req.query[] and req.params
----------------------------------------------------

req.params contains route parameters (in the path portion of the URL), and req.query contains the URL query parameters (after the ? in the URL).

You can also use req.param(name) to look up a parameter in both places (as well as req.body), but this method is now deprecated.

Given this route

app.get('/hi/:param1', function(req,res){} );
and given this URL http://www.google.com/hi/there?qs1=you&qs2=tube

You will have:

req.query

{
qs1: 'you',
qs2: 'tube'
}
req.params

{
param1: 'there'
}

+++++
You should be able to access the query using dot notation now.

If you want to access say you are receiving a GET request at /checkEmail?type=email&utm_source=xxxx&email=xxxxx&utm_campaign=XX and you want to fetch out the query used.

var type = req.query.type,
email = req.query.email,
utm = {
source: req.query.utm_source,
campaign: req.query.utm_campaign
};
Params are used for the self defined parameter for receiving request, something like (example):

router.get('/:userID/food/edit/:foodID', function(req, res){
//sample GET request at '/xavg234/food/edit/jb3552'

var userToFind = req.params.userID;//gets xavg234
var foodToSearch = req.params.foodID;//gets jb3552
User.findOne({'userid':userToFind}) //dummy code
.then(function(user){...})
.catch(function(err){console.log(err)});
});
==============
How many ways we can iterate the objects and arrays in javascript (ES6)?

// 1
array.forEach(() => { ... });
// 2
array.map(() => { ... });
// 3
array.reduce(() => { ... });
// 4
for (let i = 0; i < array.length; i++) { ... } // 5 for (let i in array) { if (!array.hasOwnProperty(i)) continue; ... } // 6 for (let i of array) { ... } =================== Var, let and const- what's the difference? https://dev.to/sarah_chima/var-let-and-const--whats-the-difference-69e

Leave a Reply

Your email address will not be published. Required fields are marked *