Js plus express plus mongo

  node.js, question

Recently, I am studying node.js, and I would like to use node.js plus express plus mongo to build a system to learn the knowledge of the front end. When choosing a database, I use mongo. So I wrote the following code:

 * student management function module
 * document reference: doc/dbtables/student.md
 * The supported operations are: add, edit, modify and batch delete
 var express = require('express');
 var router = express.Router();
 var init = function(callback) {
 var MongoClient = require('mongodb').MongoClient;
 var url = 'mongodb://localhost:27017/test_system';
 MongoClient.connect(url, function (err, db) {
 assert.equal(null, err);
 console.log("Connected successfully to server");
 var collection = db.collection('students');
 var cal = function(collection) {
 router.get('/', function(req, res, next) {
 collection.find({}).toArray(function(err, students) {
 if (!  err) {
 res.render('students', {students: students});
  1. Not familiar with the development mode of node.js plus express, when I use database query here, I must first connect to the successful database, and then use callback to call the normal POST/GET method. this is not very flexible.

  2. Is there a better solution, for example, database access is not asynchronous.


  1. In node.js plus express plus mongo, can anyone who uses them in actual projects give an overview of how to write modules?

A good solution is to write REST interfaces for database operations using back-end Java or PHP, for examplehttps://api.myapp.com/v1.0/class/student_id, and then call this interface directly at Node.