Top Banner
patrickbuergin.com 2014-12-04 Kickstarting Node.js Projects with
56
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Kickstarting Node.js Projects with Yeoman

patrickbuergin.com2014-12-04

Kickstarting Node.js Projects with

Page 2: Kickstarting Node.js Projects with Yeoman

xkcd.com/910

2

Page 3: Kickstarting Node.js Projects with Yeoman

It Can Be Hard to Get Started

‣ Naming things

‣ Searching for best practices

‣ Settling for a project structure

‣ Setting up build scripts

‣ Managing dependencies

‣ Writing boilerplate code

…TL; DR

No Consensus

3

Page 4: Kickstarting Node.js Projects with Yeoman

Yeoman Overview

Yeoman helps you kickstart new projects, prescribing best practices and tools to help you stay productive.

yeoman.io

Weekly #downloads @ npm

4

Page 5: Kickstarting Node.js Projects with Yeoman

Yeoman It’s a Workflow…

Scaffolding Package Management Build System

yo Bower & npm Grunt / Gulp

5

Page 6: Kickstarting Node.js Projects with Yeoman

Yeoman …and an Ecosystem of Generators

6

Page 7: Kickstarting Node.js Projects with Yeoman

Yeoman …and an Ecosystem of Generators

6

Page 8: Kickstarting Node.js Projects with Yeoman

$ npm install -g yo $ npm install -g generator-webapp

Front-End Example Launching a Generator

7

Page 9: Kickstarting Node.js Projects with Yeoman

Front-End Example Generator Output

+ Minification, Testing, Linting, …

$ grunt serve

8

Page 10: Kickstarting Node.js Projects with Yeoman

Scaffolding Package Management Build System

yo Bower & npm Grunt

Front-End Example Workflow

9

Page 11: Kickstarting Node.js Projects with Yeoman

Yeoman Isn’t Limited to Front-End

10

Page 12: Kickstarting Node.js Projects with Yeoman

Yeoman Isn’t Limited to Front-End

10

Page 13: Kickstarting Node.js Projects with Yeoman

Some Related Concepts

Eclipse (Java)

ArchetypesExpress Generator

&

new  &  generate11

Page 14: Kickstarting Node.js Projects with Yeoman

Popular Node Generators

express

kraken

node

Weekly #downloads @ npm

builds upon Express and enables environment-aware, dynamic configuration, advanced middleware capabilities, security, and app lifecycle events.

“based on the express command line tool”

12

Page 15: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Let’s create a small node module with the help of generator-node…

Page 16: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

After a number of questions, it initializes a suitable project structure

Page 17: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

After a couple of seconds we’re ready to go.

Page 18: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Among other things, it created a README template, as well as a build script that runs a linter (JSHint) and tests (Mocha)

Page 19: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

This is where you would add the module functions…

Page 20: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

…and this is the corresponding test

Page 21: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

For the sake of demonstration, let’s implement a module function that generates fancy job titles — just like this page does

Page 22: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Let’s adapt the test accordingly…

Page 23: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

…and implement a suitable generate function

Page 24: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

We’re good to go.Let’s run the build script and pack the module

Page 25: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Let’s use this opportunity to check out generator-express as well.I choose the most basic options…

Page 26: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

This, again, only takes a couple of seconds and initializes a basic express project structure for us

Page 27: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

By default, the build script starts the server and will automatically restart it whenever you change the code

Page 28: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

This is what the server returns by default

Page 29: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Let’s stop the server and integrate the node module we’ve just created…

Page 30: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Here you see the generated project structure, as well as the app.js file.

Page 31: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

Let’s edit the index page, so that it returns a list of fancy job titles.The corresponding files are just where you would expect them

Page 32: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

That should do.

Page 33: Kickstarting Node.js Projects with Yeoman

Demo generator-node + generator-express

And here’s the result. This example was kinda far from practice, but I guess you got the idea of what Yeoman generators can do for you.

Page 34: Kickstarting Node.js Projects with Yeoman

The MEAN Stack

Node.js + Express

Angular.js

HTML / CSS

REST API socket.io

server

client

MongoDB Wire

Mongoose ODM

32

Page 35: Kickstarting Node.js Projects with Yeoman

The MEAN Stack

Node.js + Express

Angular.js

HTML / CSS

REST API socket.io

server

client

MongoDB Wire

Mongoose ODM

+ Testing, Authentication, Preprocessors, …

32

Page 36: Kickstarting Node.js Projects with Yeoman

Popular MEAN Generators

angular-fullstack

meanjs

mean-seedmeanstack

Weekly #downloads @ npm

33

Page 37: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

You know the deal…

Page 38: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

After we answer a number of questions, the generator initializes a fairly complex project structure

Page 39: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

Let’s start a MongoDB server so that we can check out the application…

Page 40: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

…then run grunt to build and start it

Page 41: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

This is the default page. As you can see on the upper right, the scaffold already includes some basic user management functionality

Page 42: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

Let’s register a new user. In order to use an external identity provider, we would have to add API keys and host the server on the internet (callbacks).

Page 43: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

After logging in, you can find some menu items to manage your profile

Page 44: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

The generated code is structured in a comprehensible manner. In the foreground, you see a part of the default schema for users (Mongoose)

Page 45: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

The generated code is structured in a comprehensible manner. In the foreground, you see a part of the default schema for users (Mongoose)

/config (back-end) Authentication Strategies, API Keys, Addresses, …

/app (back-end) Express components, Mongoose Schemas

/public (front-end) AngularJS modules, view templates, …

Page 46: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

generator-meanjs also offers a number sub-generators in order to speed up your development — for example :crud-module

Page 47: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

It creates both AngularJS and Express files, supporting full CRUD (create, read, update, delete) functionality, as well as basic test generation (Karma & Mocha).

Page 48: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

This is what our new CRUD module looks like in the front-end

Page 49: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

As promised, there is a list view, a detail view and views for adding/editing objects

Page 50: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

Let’s add a new job…

Page 51: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

There it is — it has also been written to our database by means of a POST request to the corresponding API endpoint provided by Express

Page 52: Kickstarting Node.js Projects with Yeoman

Demo generator-meanjs

Here is an excerpt of the generated code — a great starting point

Page 53: Kickstarting Node.js Projects with Yeoman

Other Back-End Generators

jhipster

skinny

Weekly #downloads @ npmTotal #downloads ’14 @ npm

“Skinny framework’s concept is Scala on Rails”49

Page 54: Kickstarting Node.js Projects with Yeoman

Using Yeoman to Help Others Get Started

50

As we looked for a way to help the community build and deploy production level MEAN applications, we decided to go with a Yeoman generator. […] provides a powerful, easy to maintain, and open solution for scaffolding applications.

Page 55: Kickstarting Node.js Projects with Yeoman

Building Generators

51

Well documented @ yeoman.io/authoring

…and there is a generator for that :)

Page 56: Kickstarting Node.js Projects with Yeoman

Discussion

Bloat

Outdated Generators

Doesn’t fit your needs

Agility

Focus on issue at hand

Helping others

Inspiration

Not invented here