Using mongoose.model without schema param - node.js

I'm working through this Node ToDoList App API tutorial. It has one model, one controller and one routes file:
https://www.codementor.io/olatundegaruba/nodejs-restful-apis-in-10-minutes-q0sgsfhbd
Repo:
https://github.com/generalgmt/RESTfulAPITutorial
In the model, we use mongoose to define TaskSchema and export mongoose.model('Tasks', TaskSchema);
In the controller, we create a Task var, set equal to mongoose.model('Tasks', TaskSchema); and use it to define several controller methods.
The server.js requires Task from the model, but never seems to use it for anything. The server also requires the routes file, which in turn require the controller, but I can'a see how they ever interact with the model.
How does the rest of the app know about the model? How does the controller know the schema for Task? Is this all mongoose magic?

The Task schema is being called in the controller in line #4 https://github.com/generalgmt/RESTfulAPITutorial/blob/master/api/controllers/todoListController.js#L4
It does seem like the model being required in server.js is not used.
Server.js or routes don't need to interact with the schema, as all the methods required to interact with the schema are required in the Task constructor. The controller knows about the Task schema because it is being required in the controller.

Related

How to implement `jwt` using express js in mvc pattern?

I have gone through this link. How can I implement this in mvc pattern?
This is my file structure
config
|----mongo.js //connection to mongoose
|----routes.js //all routes of all controllers
controllers
|----controller.js //all controller files
models
|----model.js //all models nothing but schemas of different collections
package.json
server.js
How to implement jwt in the above pattern so that it can be applied to all routes. Please help me out.

Sequelize from routes?

How can I access sequelize from other routes ?, all the tutorials I've seen use sequelize from app.js, but if I want to use a model from other route I'll need to initialize it everytime. how can I initialize sequelize globally and call it from other routes ?
Kenny, try to see this example from sequelize's site: Usage with Express.JS, it's very easy to understand, and the good thing is that using this approach you can singleton all your models, it load the models when starting the application and then use the require('models') to grab them all and use it everywhere you like (globally).

JavaScript Requires and Exports in Node.JS

I am writing a Node.JS REST API using Express, and I was a little confused about the require() and exports statements in Node.JS.
For instance, let's say I am writing a simple app wherein app.js contains the basic app.get statements, routes.js contains the functions passed as a callback to those app.get statements and events.js contains the Mongoose Schemas and Models.
Now, if routes.js requires events.js, can I call Model.find() and functions like those in routes.js and if yes, what exports will I have to make from events.js?
No need to export anything from model. Just require it.
In routes.js, you can access your model using mongoose.model('Schema')
routes.js
require('./events.js')
var mongoose = require('mongoose')
, Model = mongoose.model('myModel')

Want to get crystal clear about NodeJS app structure (Full JavaScript Stack) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I would like to know the structure of a typical NodeJS app, because the more I read and see the projects, the more confused I am, specifically for questions like these (or even more after I updated this question):
Take the MEAN stack for example, from what I know, NodeJS and Express take care of the server part, providing the server interface, etc. MongoDB and Angular are pretty straightforward.
But where should the business logic go? Say if I have a controller.js which contains a function, and the route.js file binds the request with this controller function. My question is: under which module these files belong to/run under (Express or NodeJS?)
Where is the starting point of a NodeJS app? Say index.php is the starting point of a PHP app, but where is it for NodeJS app? I can see that all Nodejs projects have a file called server.js or app.js, etc.(containing something like module.exports = app;) But how can NodeJS know which file to find and execute?
I am a fresh noob on NodeJS, Express, sequelize.js/Mongoose, Jade/EJS but want to get started on a NodeJS project. Could you please elaborate on the actual function that each modules provide and a general introduction of the typical structure for a full JS stacked NodeJS app? Thanks in advance!
Alright, this is a pretty broad question and I'm definitely no expert, but I'll do my best here.
TL;DR
routes are controllers that tell what logic to execute when a user navigates their browser to a certain path within your app, including which views to render and what data to send to those views
models are just that - data models within your application
module.exports = tells a file what exactly it "exports", that is what code needs to be executed or accessible from your main app file.
require(..) includes a module. You can set this on a variable so that you may call module functions later, or simply execute a function if that is all that module.exports returns.
Combining these techniques can help you nail down a solid framework for any of your applications.
Long Answer
Express provides a solid framework for structuring your Node.js application. Node is completely independent of Express, but because of how popular Express is they practically go hand-in-hand. Once installed, Express can be used to generate a scaffold web project (with options) for you to build on top of if you'd like.
Controllers
A generated project will create /routes/index.js, which (if you understand MVC) is essentially your main controller. A route in express is written as so:
app.get('/path', function(req, res, next){ .. } );
Lets break that down: our application variable (app) is being told that on a GET request to '/path' to execute an anonymous callback function with req, res, next variables (request, response, callback respectively). I find it helpful to think of this like a custom event handler.
Its important to note at this point that we could also call app.post with the same syntax for posts to a URL as opposed to gets.
Within our anonymous callback, we handle any incoming data and render a view for the user. This is where most of my business logic ends up, so it actually makes sense to NOT use anonymous functions here. Here's an example of a basic callback that just displays a homepage:
app.get('/', function(req, res, next){
//some business logic
res.render('views/home');
});
When the user tries to GET the index path of our application (/), we simply render our home view that, from the root of our project, is stored in a views folder.
But what if we want to modularize this so that we aren't declaring all of our routes in our main app.js or server.js?
We use module.exports = .. in our modules to tell our server what exactly to include. In my controller, I export a single function that takes the application as an argument and uses that to define our routes like so:
Controllers/User.js
module.exports = function(app){
app.get('/users', function(req, res){
var users = req.db.collection('users').find();
if (!users) {
console.log("no users found");
res.redirect('/');
} else {
res.render('users/index', {users : users});
}
});
};
Don't worry about the req.db code, I attach the database to the request in my application but that isn't done by default. Simply understand that I'm getting a list of 'users' here, and redirecting the user to the index of my app if there aren't any.
Models
Mongoose provides us with a great interface for writing models. With mongoose, writing models is a three step process:
Define a schema
Define model logic
Generate and export the model
Here is an example of a User model:
Models/User.js
var mongoose = require('mongoose'),
userSchema = new mongoose.Schema({
name: { type: String, required: true },
joinDate: {type: Date, default: date.now }
}),
User = mongoose.model('user', userSchema);
module.exports = user;
Server App
module.exports is used to help us define some modularity to our codebase. When we run a node application, we're ultimately running a single JavaScript file (you've already seen that file with server.js or app.js).
To keep this file from getting too big with multiple models and routes, we use require(module) to include code from other JS files. module in our case would be a path to the module we want to require. If you have the following doc structure:
| Controllers
- User.js
| Models
- User.js
| Views
app.js
To include your user controller from app.js, you would write: require('./Controllers/User'). Since our controller modules simply export functions, we can call that function immediately after our require statement by simply adding parentheses at the end (with whatever parameters are required). Including my controllers looks like so:
require('./Controllers/User')(app)
I'm passing in the actual app, because my module (below) simply exports a function that adds business logic to my app's routes. This only needs to be called and never used, so I don't capture my controller as a variable to call methods on later.
Including models is a little different, since we may want to perform some operation that our model defines. We can do this by changing up our require code just a bit:
var User = require('./Models/User');
Now we can call methods of our User model whenever. Mongoose gives us a lot of base functionality for free:
User.find({}, function(err, users){ .. });
The above function will go find all of our users, and then execute an anonymous function with a potential err (is null if no issues) and then a list of our users in JSON format. Pretty nifty.
Combining all of these concepts is how you create a basic web application using Express and Node.js. Please let me know in the comments if there's anything I can clarify about how I use Express. This is very surface level knowledge, and I suggest digging into documentation and looking at plugins to extend the capabilities of your apps. Good luck!

How manager My models with mongoose node.js and express?

I've a problem for organize my models.
first I get this error:
User is not defined
and when I include in routes.js my model, like this:
var User = require('../models/user');
I get this error:
Cannot overwrite User model once compiled.
There is my repot corresponding to my first error.
ty for your assist.
Regarding your first issue, when you want to retrieve your model its better to use Mongoose#model. For example
For example:
require('../models/user');
var mongoose = require("mongoose"),
User = mongoose.model("User");
User.find({ // ...now you can use the User model
Regarding your second issue, you only have to require user.js once to compile your model. When you include it in route.js, it's being loaded twice in your app (app.js requires it too, looking at your repo).
If you're not sure how to organise your models, use other Express/Mongoose apps as a starting point. I've used this repo in the past. Relevant instance of organising and compiling your models are in server.js and /app/models

Resources