Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js & express - global modules & best practices for application structure
    text
    copied!<p>I'm building a node.js app that is a REST api using express and mongoose for my mongodb. I've got the CRUD endpoints all setup now, but I was just wondering two things.</p> <ol> <li><p>How do I expand on this way of routes, specifically, how do I share modules between routes. I want each of my routes to go in a new file, but obviously only one database connection as you can see i've included mongoose at the top of people.js.</p></li> <li><p>Do I have to write out the schema of the model 3 times in my people.js? The first schema defines the model, then I list all the vars out in the createPerson and updatePerson functions. This feels like how I made php/mysql CRUD back in the day lol. For the update function, I've tried writing a loop to loop through "p" to auto detect what fields to update, but to no avail. Any tips or suggestions would be great.</p></li> </ol> <p>Also, I'd love any opinions on the app as a whole, being new to node, it's hard to know that the way you are doing something is the most efficient or "best" practice. Thanks!</p> <p>app.js</p> <pre><code>// Node Modules var express = require('express'); app = express(); app.port = 3000; // Routes var people = require('./routes/people'); /* var locations = require('./routes/locations'); var menus = require('./routes/menus'); var products = require('./routes/products'); */ // Node Configure app.configure(function(){ app.use(express.bodyParser()); app.use(app.router); }); // Start the server on port 3000 app.listen(app.port); /********* ENDPOINTS *********/ // People app.get('/people', people.allPeople); // Return all people app.post('/people', people.createPerson); // Create A Person app.get('/people/:id', people.personById); // Return person by id app.put('/people/:id', people.updatePerson); // Update a person by id app.delete('/people/:id', people.deletePerson); // Delete a person by id console.log('Server started on port ' + app.port); </code></pre> <p>people.js</p> <pre><code>//Database var mongoose = require("mongoose"); mongoose.connect('mongodb://Shans-MacBook-Pro.local/lantern/'); // Schema var Schema = mongoose.Schema; var Person = new Schema({ first_name: String, last_name: String, address: { unit: Number, address: String, zipcode: String, city: String, region: String, country: String }, image: String, job_title: String, created_at: { type: Date, default: Date.now }, active_until: { type: Date, default: null }, hourly_wage: Number, store_id: Number, // Inheirit store info employee_number: Number }); var PersonModel = mongoose.model('Person', Person); // Return all people exports.allPeople = function(req, res){ return PersonModel.find(function (err, person) { if (!err) { return res.send(person); } else { return res.send(err); } }); } // Create A Person exports.createPerson = function(req, res){ var person = new PersonModel({ first_name: req.body.first_name, last_name: req.body.last_name, address: { unit: req.body.address.unit, address: req.body.address.address, zipcode: req.body.address.zipcode, city: req.body.address.city, region: req.body.address.region, country: req.body.address.country }, image: req.body.image, job_title: req.body.job_title, hourly_wage: req.body.hourly_wage, store_id: req.body.location, employee_number: req.body.employee_number }); person.save(function (err) { if (!err) { return res.send(person); } else { console.log(err); return res.send(404, { error: "Person was not created." }); } }); return res.send(person); } // Return person by id exports.personById = function (req, res){ return PersonModel.findById(req.params.id, function (err, person) { if (!err) { return res.send(person); } else { console.log(err); return res.send(404, { error: "That person doesn't exist." }); } }); } // Delete a person by id exports.deletePerson = function (req, res){ return PersonModel.findById(req.params.id, function (err, person) { return person.remove(function (err) { if (!err) { return res.send(person.id + " deleted"); } else { console.log(err); return res.send(404, { error: "Person was not deleted." }); } }); }); } // Update a person by id exports.updatePerson = function(req, res){ return PersonModel.findById(req.params.id, function(err, p){ if(!p){ return res.send(err) } else { p.first_name = req.body.first_name; p.last_name = req.body.last_name; p.address.unit = req.body.address.unit; p.address.address = req.body.address.address; p.address.zipcode = req.body.address.zipcode; p.address.city = req.body.address.city; p.address.region = req.body.address.region; p.address.country = req.body.address.country; p.image = req.body.image; p.job_title = req.body.job_title; p.hourly_wage = req.body.hourly_wage; p.store_id = req.body.location; p.employee_number = req.body.employee_number; p.save(function(err){ if(!err){ return res.send(p); } else { console.log(err); return res.send(404, { error: "Person was not updated." }); } }); } }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload