Crate a new Express app

At this point, you should be able to go forth and create an app. In this example, we will create a Node.js app with Express framework.

  1. $ express <your-new-app>

Running this command (using demo-app as the example), you should see the following:

  1. create : demo-app
  2. create : demo-app/package.json
  3. create : demo-app/app.js
  4. create : demo-app/public
  5. create : demo-app/public/javascripts
  6. create : demo-app/public/images
  7. create : demo-app/public/stylesheets
  8. create : demo-app/public/stylesheets/style.css
  9. create : demo-app/routes
  10. create : demo-app/routes/index.js
  11. create : demo-app/routes/users.js
  12. create : demo-app/views
  13. create : demo-app/views/index.jade
  14. create : demo-app/views/layout.jade
  15. create : demo-app/views/error.jade
  16. create : demo-app/bin
  17. create : demo-app/bin/www
  18. install dependencies:
  19. $ cd demo-app && npm install
  20. run the app:
  21. $ DEBUG=my-application ./bin/www

BOOM! Express takes care of most of the labor. Now, we do what the computer says, change directory into the app dir and run npm install.

What’s in the app?

At this point, you should be able to do a ls and see the new structure that was created for you.

  1. app.js node_modules/ public/ views/
  2. bin/ package.json routes/

app.js

This is the logical starting point for your app. Some of the things in there, lets talk about:

The following lines, for this type of app, we don’t need them:

  1. var user = require('./routes/user');
  2. app.get('/users', user.list);

Sets the path to the dir where the view files are located:

  1. app.set('views', path.join(__dirname, 'views'));

Sets the path to the dir with static assets:

  1. app.use(express.static(path.join(__dirname, 'public')));

Sets the root route for the app:

  1. app.use('/', routes);

node_modules/

This is the dir where all your npm packages will reside.

public/

Directory for all static assets like images, JavaScript, CSS, fonts, etc …

views/

Where all your layout and view Jade files will live.

bin/

There is a single file here, www and this is what activate the Node server.

package.json

Project description, scripts manager and the app manifest. Notice the following object:

  1. "scripts": {
  2. "start": "node ./bin/www"
  3. },

This is the code that allows you to run npm start for the app.

routes/

This is the directory where you will build out the RESTful routes for your app. With a base install there should be two files in there, index.js and users.js.