DEV Community

Jacob Colborn
Jacob Colborn

Posted on

Creating an Application

My first application

Starting from nothing can be daunting. When I first started, I felt overwhelmed. Oddly, that was only about a week ago (it seems so distant now). When I set out to create an actual application I had two reasons to start something so big.

  • I wanted to learn and needed to throw myself against the wall
  • I am working on a group project as the full-stack developer

The second reason may not be the best, but the first is a great way actually to learn something. Don't get me wrong, I have been learning with free resources, and I have made parts of applications, but nothing from the ground up. It can be overwhelming to look at the blank screen and realize you aren't 100% sure what things you need to pick to make sure you do a good job. What services and API's would work? You have nothing except a blinking cursor and a cursory knowledge of Node and Express.

I set off. I picked Express for HTTP. I picked MongoDB and Mongoose for my database layer. I picked PUG for my templating language. I started slinging code and seeing what I could get together. The first thing I wanted to do was get the user signup and login working. I decided to roll my own to gain experience. Here is what I came up with:

  • Setup the user schema and model in a separate file
  • Setup Express routes in a separate file based on category.
  • Create the template pages to send a form to a POST route in my user.js routing folder
  • Use express-sessions to handle the user sessions

I have just gotten this working. The way the routes work is by creating a file and then including it into your entry point app.js file:

// setup the user session
app.use(session({ secret: SESSION_SECRET });

// setup the router file
app.use('/user', require('./router/user');

This code tells app.js to use the ./router/user.js file for any routes starting with /user. From there, user.js includes the user-controller.js that is set up to interface with the database. Some routes use the controller queries if they need access to the database. For the router file, it looks something like this:

const userController = require('../controllers/user-controller');
// some other routes and code
router.post('/create', (req,res) => {
  userController.create(what to send to the database and then what to do with that)
});

In the user-controller file you would have a corresponding create() function that would handle that request.

// add the user model
const User = require('../models/user-model');

exports.create = (username,password,callback) => {
    // Create the user object with the user model
    // using the username and password from the router
    // then return the data you need from the callback
}

Once I got all the routes setup and the controller functions set up, everything worked (I needed the model for the schema, too). I was able to create a user, login, and then log out. I was able to store the user session information through a cookie.

I want to go into more detail, but this is already a long post. I'll be adding more as I learn. What I wanted to get out there is that starting your very first project that requires many systems to work together can look like driving for a brick wall. What I learned over the last week of setting this part up is that a lot of the time, that brick wall is an optical illusion on the ground and you drive right over it. I hope now, as I stare down these other brick walls, I can drive on just as effortlessly.

Top comments (0)