S
S
Stopy2015-04-14 21:10:46
MongoDB
Stopy, 2015-04-14 21:10:46

How to build my node.js+express+mongodb (router, ajax) application?

1) How are ajax requests processed in nodejs in terms of architecture? Those. in express using middleware:

var send_mail = require('./models/send_mail.js');
app.post('/send_mail', function(req,res,next){
  	send_mail(req.body, new Date());
});

Or is it done in some other way?
2) How to insert data from the database into the template?
var send = ('./models/send.js');
app.get('/send', function(req,res,next){
 res.render('main', data);
});

If so, how to return data asynchronously from send.js and insert it into the render? And if not, then how?
3) With express, you can also make some beautiful router or do you need to put middlewares directly into app.js? (I know that you can do at least the entire site in 1 file, but I'm interested in how to do it correctly and conveniently)
PS It's been 2 days in node.js, I'll forgive you not to throw stones, but to give constructive answers. Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Shikanov, 2015-04-14
@Stopy

1) Ajax requests are processed in the same way as regular requests. The send_mail function from the example should probably accept a callback:

var send_mail = require('./models/send_mail.js');
app.post('/send_mail', function(req,res){
  	send_mail(req.body, new Date(), function(err) {
    if (err)
      res.send({result: false, message: err.message}); // отправляется json
    else
      res.send({result: true});
  });
});

2) Access to the database in node.js is almost always asynchronous, so the database driver provides a callback to receive data, which your send function should also accept:
var send = require('./models/send.js');
app.get('/send', function(req,res){
  send(function(err, data) {
    if (err) throw err; // или как-то по другому обрабатывать
    res.render('main', data);
  });
});

3) In one file, you can only write a very simple application, well, or some example. In real projects, everything is divided into modules, there are a lot of examples on GitHub .
----
The send function should look something like this:
function send(cb) {
  // ... соединение с БД предполагается в переменной db
  var collection = db.collection('documents');
  collection.find({}).toArray(cb);
}

K
Konstantin Kitmanov, 2015-04-14
@k12th

1) AJAX requests are always and everywhere processed in exactly the same way as regular ones. What is your question - how to distinguish AJAX from non-AJAX? how to send JSON to client?
2) Something like this:

var send = require('./models/send.js');
app.get('/send', function(req, res) {
  send(function (err, data) {
    if (err) {
      return res.send(500, 'Error happened')
    } else {
      res.render('main', data);
    }
  })
});

Adjusted for callback-hell, if you need to go to the database many times :) (decided by the async module).
3) Why in one file? routes, controllers, service middleware - all this can and should be scattered across different files and even different folders. In app.js, an instance is only created and settings and routes are fed to it.
PS Have a look at https://github.com/madhums/node-express-mongoose-demo , perhaps some of the questions will clarify this. Just do not copy thoughtlessly, there are controversial decisions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question