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
Yeoman helps you to kickstart new projects, prescribing best practices and tools to help you stay productive.
petecoop/generator-express
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:
Install Yeoman:
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
Install bower as it is used to manage static dependencies like JavaScript:
npm install -g bower
Run the yo command to create your project:
yo express
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:
package.json
Contains project information and its dependencies
config/config.js
Contains information like database settings, root path of project, port of server etc.
app.js
Starts your server at a given port
<controllers>
This directory contains all the controller files that maps routes to functions.
<models>
This directory contains all the models that represents entities or tables in database
<views>
This directory contains all the front end files containing HTML.
<public>
This directory contains all the static resources like css, js, images etc.
Now execute following command to download project dependenceis:
npm install
The project is ready, let us now run our server from the command prompt/terminal. Execute the command:
node app.js
You can check your server on a web browser at following URL:
http://localhost:3000
The default port for the server is 3000 defined in config/config.js file.