Friday, February 17, 2017

ExpressJS - Scaffolding

Scaffolding allows us to easily create a skeleton for a web application. We manually created our public directory, added middleware, created separate route files, etc. A scaffolding tool sets up all these things for us so that we can directly get started with building our application.

The scaffolder we'll use is called Yeoman. It is a scaffolding tool built for Node.js but also has generators for several other frameworks(like flask, rails, django, etc). To install yeoman, enter the following command in your terminal:
npm install -g yeoman
Yeoman uses generators to scaffold out applications. To check out the generators available on npm to use with yeoman, head over here. For the purpose of this tutorial we'll use the 'generator-express-simple'. To install this generator, enter the following command in your terminal:
npm install -g generator-express-simple
To use this generator, enter:
yo express-simple test-app
Then it'll ask you simple questions about what things you want to use with your app. Select the following answers, or if you already know about these technologies then go about choosing how you want them to be.
express-simple comes with bootstrap and jquery
[?] Select the express version you want: 4.x
[?] Do you want an mvc express app: Yes
[?] Select the css preprocessor you would like to use: sass
[?] Select view engine you would like to use: jade
[?] Select the build tool you want to use for this project: gulp
[?] Select the build tool you want to use for this project: gulp
[?] Select the language you want to use for the build tool: javascript
   create public/sass/styles.scss
   create public/js/main.js
   create views/layout.jade
   create views/index.jade
   create views/404.jade
   create app.js
   create config.js
   create routes/index.js
   create package.json
   create bower.json
identical .bowerrc
identical .editorconfig
identical .gitignore
identical .jshintrc
   create gulpfile.js


I'm all done. Running bower install & npm install for you to install the required dependencies. If this fails, try running the command yourself.
It'll then create a new application for you, install all the dependencies, add few pages to your application(home page, 404 not found page, etc.) and give you a directory structure to work on.
This generator creates a very simple structure for us. Explore the many generators available for express and choose the one that fits you right. Steps to working with all generators is the same. You'll need to install a generator, run it using yeoman, it'll ask you some questions and then create a skeleton for your application based on your answers.

No comments:

Post a Comment