A generator is a software tool that helps us create a project with directory structure based on best practices and standards.
This also allows multiple developers to work together and understand what and how others are doing.
There are various generator software available in market for different kind of technologies.
Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.
generator-express is one of the popular generator that helps us to create our Node.js MVC project using Express on the fly.
Here are the steps:
npm install -g yo
The -g switch will install yo in the global namespace instead of our project.
Install generator-express plugin:
npm install -g generator-express
npm install -g bower
Run the yo command to create your project:
Now follow the on-screen instructions. Choose the following settings for now:
- Would you like to create a new directory for your project? Yes
- Enter directory name node-project
- Select a version to install: MVC
- Select a view engine to use: EJS
- Select a css preprocessor to use (Sass Requires Ruby): None
- Select a database to use: None
- Select a build tool to use: Gulp
Your project directory should be created now and the structure looks like this:
<node-project> <app> <controllers> <models> <views> <config> config.js express.js <node_modules> <public> <components> <css> <img> <js> app.js bower.json gulpfile.js package.json
Here are some important files/folders:
Contains project information and its dependencies
Contains information like database settings, root path of project, port of server etc.
Starts your server at a given port
This directory contains all the controller files that maps routes to functions.
This directory contains all the models that represents entities or tables in database
This directory contains all the front end files containing HTML.
This directory contains all the static resources like css, js, images etc.
Now execute following command to download project dependenceis:
The project is ready, let us now run our server from the command prompt/terminal. Execute the command:
You can check your server on a web browser at following URL:
The default port for the server is 3000 defined in config/config.js file.