First experience in node.js Is it possible to get a decent result without reworking from scratch?

Question:

My first programming experience, except for student labs in Delphi (and those were a very long time ago).

Purpose: an "application" that on the first page produces a clickable list of user names (we take the names in mongodb), and when a user is clicked, it redirects to another page, where it already displays (taking from the database) detailed information about the selected user. Thoroughly rewrote, got this.

Questions:

  1. Is the overall structure more or less adequate? Refine what you already have or rebuild from scratch?
  2. In the userModel module, I connect to the database. It provides for the simplest error handling in the form of a message to the console. How can I make this error go to express so that I can display the error object in the browser?
//app.js:

var express = require('express');
var app = express();

var router = require('./router/router');
app.use(router);
app.listen(3000, function() {
  console.log('Listening on port 3000!\nDatabase on mlab.com');
});

//router.js:
var express = require('express');
GetFirstPage = require('../lib/GetPages').firstPage;
GetSecondPage = require('../lib/GetPages').secondPage;

var router = express.Router();
router.get('/', GetFirstPage);
router.get('/user?:id', GetSecondPage);
router.use(express.static('public'));
module.exports = router;
//userModel:
const mongoose = require('mongoose');
mongoose.connect(require('../credentials').mongo, function(err) {
  if (err) console.log('Error database connection\n', err)
});


const userShema = mongoose.Schema({
  name: String,
  age: String,
  disciplines: String,
});

module.exports = mongoose.model('User', userShema);

//GetPages.js:
var express = require('express');
var app = express();
var _ = require('underscore');
const User = require('./exportFromDB').user;
const NamesList = require('./exportFromDB').namesList;

module.exports.firstPage = function(req, res) {
  Promise.all([
    NamesList(), readMainPage()
  ]).then(function(results) {
    const list = results[0].map(function(item) {
      return item.name
    })
    res.send(results[1]({
      'list': list
    }))
  })
}

module.exports.secondPage = function(req, res) {
  Promise.all([
    User(decodeURIComponent(req.query.id)), readSecondPage()
  ]).then(function(results) {
    res.send(results[1]({
      name: results[0][0].name,
      age: results[0][0].age,
      disciplines: results[0][0].disciplines
    }))
  })
}

function readMainPage() {
  const myFile = require('../lib/exportFile').mainPage;
  return myFile();
}

function templateMainPage(template) {
  res.send(template({
    'list': List
  }))
}

function readSecondPage() {
  const myFile = require('../lib/exportFile').secondPage;
  return myFile();
}
//ExportDB.js
userModel = require('./userModel');


module.exports.namesList = () => {
  return new Promise(function(resolved, rejected) {
    userModel.find({}, 'name -_id', function(err, data) {
      resolved(data)
    })
  })
}
module.exports.user = (url) => {
  return new Promise(function(resolved, rejected) {
    userModel.find({
      name: url
    }, function(err, data) {
      resolved(data)
    })
  })
}

Answer:

Somehow, the request for the page of all users looks like this:

app.get('/', function(req, res) {
  users.find({}, (err, docs) => {
    res.locals.users = docs;
    res.render('list');
  };
};

And in the list page template, users are displayed in the each user in users loop, where each is wrapped in a link with an attribute like this href="/users/#{user._id}" (or whatever template syntax you have)

And the request for pages of individual users is app.get('/users/:id', ... , and in its handler, in the request object, the value of the req.params.id parameter will be the user's ID in the database, by which it is easily requested –

app.get('/users/:id', function(req, res) {
  users.find({_id: req.params.id}, (err, result) => {
    res.locals.user = result;
    res.render('user');
  };
};

The page template received a user object – user._id user.name user.age and so on. All in all – thanks to TJ Karavaichuk (or whatever) for Express.

Scroll to Top