Module.Exports

Woo Hoo! We are going over how to clean up our code and make it more readable/usable. I am stoked for this section of the course because my Milk Log App is a filthy mess right now and it feels like a stressful place to work.

Modular Code will scale better and is easier to share across applications. This is a something that is worthwhile getting into a habit of doing early on – like using git, getting comfortable using the terminal, setting up different enviornments and so on. These things are much easier when you start with small, low risk projects and it lays the ground work for when you get your first job, or start a big personal project.

I’ve been taking the chuck myself right into the action approach.. which leads to lots of mistakes learning opportunities. Then once I’m feeling a balance of overwhelmed and accomplished I head back over to course work, or other resources to learn how I could have done things better and easier.

Anyway I’m getting off topic.. TL;DR Don’t be afraid to try things – even if they seem unneccesary at the scale you’re working. Everything is an oppurtunity to learn!

For this explanation we are using the blog user and posts example from my last post. The first thing that we want to do is take the schema for the user and the post and put them into their own ejs files.

From inside my working directory I’ve created a new directory named modules and inside of that directory I’ve created post.ejs and user.ejs.

$ mkdir modules
$ touch modules/post.ejs
$ touch modules/user.ejs

Now I am cutting the post schema and model definition from my main file and moving it to the post file. Mongoose needs to be required in this file as well. I have changed the model definition slightly so that when post.ejs is required by my main file the Post model will be returned.

postSchema

Do the same thing for user.ejs with the user schema and model.

To require user and post in our main file we simply add the lines:

var Post = require("./models/post");

var User = require("./models/user");

The ./ indicates from the current directory.

Module.exports will alow us to break things down into separate files. This will improve readablity and allow us to use parts of our application in different places!!

That’s all on this topic for now! In the next lesson I’ll be using Module.exports to clean up Yelp Camp, a larger application we have been working on in the Web Developer Boot Camp. I apologise again for starting in the middle and not providing a ton of context, but I promise it will get better as I go! This blog’s purpose is to increase my understanding of the concepts that I am learning – so please bring on the feedback, tips, criticisms and of course, support!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: