Scaffolding the application

As already mentioned before, we use as tools as possible that streamline our development process of web applications.

We will start from scratch, using a Yeoman generator called MEAN.JS.

Let's begin; remember that you can download the sample files and follow our evolution. However, I strongly recommend that you follow refactoring step by step for a better understanding.

  1. Create a folder called meanjs-generator, open the terminal inside it, and type:
    npm install -g generator-meanjs
    

    The -g key means general, and you'll need to be the administrator of your machine. Now, we have what we need: the generator.

  2. Type the yo meanjs command and follow the instructions and fill in the answers; the text in light blue is the answer that you must complete, as shown in the following screenshot:
    Scaffolding the application

    If you accompany the process on your terminal, you can see that the Bower also already executed these commands and installed all the AngularJS scripts.

    Tip

    All Yeoman generators have their own configuration options, and they are very easy to follow.

  3. The next step is to run the node server command. Probably, you should receive an error message on your terminal, with some URLs pointing to MongoDB folders. In fact, it is predictable because it indicates that MongoDB is not running.
  4. Then, open the server.js file on the application root and add a treatment for this error.
  5. Include the following lines directly after the var db = mongoose.connect(config.db); line:
    // Add a warning message if MongoDB don't running
    mongoose.connection.on('error', function() {
        console.error('MongoDB Connection Error. Make sure MongoDB is running.'),
    });

    As shown in the following screenshot:

    Scaffolding the application
  6. Now, run the node server.js command again. We can clearly see what happened:

    MEAN.JS application started on port 3000

    MongoDB Connection Error. Make sure MongoDB is running.

  7. Start MongoDB. Run the command again and open your browser in the localhost:3000 URL.

The application is ready to be refactored; if everything went well so far, you can see the welcome message. On this page, it is even possible to access the links for each technology used and also the MEAN.JS documentation.

The application anatomy

This generator has a very important peculiarity. Usually, when we work with web applications using AngularJS, by default all application files reside in the app directory. However, in this case, this directory stores files regarding our API, for example, server files, which are usually stored in the server folder.

We can clearly see the separation and organization of our code in folders such as controllers, models, routes, tests, and views. There is nothing new up to now because we have already used this same structure in previous chapters; the only difference is the addition of the tests directory, as shown in the following screenshot:

The application anatomy

Note that the suffix, server, is present in the naming of all the files; it greatly facilitates the work of the team and became highly scalable. The public folder contains all files related to the frontend application. Note that each module has its own folder, in addition to all AngularJS files that are housed in the lib folder. Each module has its own internal structure to their respective folders: config, controllers, services, tests, views, and a file in the root that registers the module in the application, as shown in the following screenshot:

The application anatomy

We cannot say that this is a simple structure. On the contrary, it is a complex but very well-divided structure. Finally, we have the config folder and two other folders inside it, env and strategy; the latter contains the configuration files of the Passport module to deal with authentication and user's control over Facebook, Google, LinkedIn, Twitter, and local access with a username and password.

The env directory holds settings, access to static files, template rendering, and session control in the all.js file. The other files store the connection string to the database and also the access keys for social networking APIs.

The application anatomy

We shall now see briefly what each file's root folder is:

  • .bowerrec: As we saw before setup, this is a file to receive Bower components
  • .gitignore: This file doesn't track files on the control version
  • bower.json: This file sets up the dependencies for the frontend side, such as Angular, jQuery, and Bootstrap
  • gruntfile.js: This file is a task runner for building, testing, development, and other common tasks
  • karma.conf.js: This file sets up the testing environment
  • license.md: This file shows which license is used
  • package.json: This sets up the application dependencies for server side, such as Express and Passport
  • procfile: This sets up an environment for cloud deployment
  • readme.md: This has information about the application
  • server.js: As we saw before, this is the heart of the Node server for the application

We note that in this way, we can scale the application in a practical way. As we saw in the previous examples, we are ready to proceed with the refactoring. We will divide it into two stages: the first to the API, including a service for inclusion of speakers, and the second to create the frontend itself with AngularJS.

However, we can choose to create everything from scratch, including file-by-file directories in the backend and the frontend too. However, once you have already used the generator to create all this structure, use a subgenerator for creation of two steps (backend files and frontend files) simultaneously. This way, we save time and become more productive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.172.93