This project is no longer maintained. Please visit vulgar to follow the new version, and check out our new command line tool for generating and managing cutting-edge MEAN stack apps at vulgar-cli
A MEAN stack development kit featuring Angular 2 (Router, Forms, Http, Services, Tests, E2E), Express, MongoDB (complete with Mongoose), Node, PassportJS, Socket.IO, Karma, Protractor, Jasmine, Istanbul, TypeScript, Typings, Sass, Docco, TsLint, Ng2Lint, Hot Module Replacement, and Webpack, as well as ES6/ES7 support for the back-end by datatype_void.
Walk through a complete tutorial that shows you how to build a simple todo app using this framework, check out Building A Single Page Todo App with MEAN--Including Angular 2
If you're looking to learn about Webpack and ES6 Build Tools check out ES6-build-tools
If you're looking to learn TypeScript see TypeStrong/learn-typescript
If you're looking to learn how to move your Angular 1.x directives over to Angular 2 see Migrating Directives to Angular 2
And always keep the Angular 2 docs at hand, because the times are always changing
This seed repo serves as an MEAN starter for anyone looking to get a MEAN fullstack app up and running with Angular 2, TypeScript(on the front-end), and ES6/ES7 (on the back-end) fast. Using Webpack for building our files and assisting with boilerplate. We're also using Protractor for our end-to-end story and Karma for our unit tests.
- Best practices in file and application organization for Angular 2.
- Ready to go build system using Webpack for working with TypeScript.
- Hot module reloading for the front-end Ă la Webpack.
- Angular 2 examples that are ready to go when experimenting with Angular 2.
- A great MEAN seed repo for anyone who wants to start their project.
- Testing Angular 2 code with Jasmine and Karma.
- Coverage with Istanbul and Karma
- End-to-end Angular 2 code using Protractor.
- Type manager with Typings
- Sass preprocessor linting and compiling
- Automatic documentation for all project related Sass, TypeScript, and JavaScript files with Docco; front-end and back-end
The rest of the stack features:
- Express ready for ES6/ES7 code through transpiling with Babel 6,
- Socket.io for real time event-based communication.
- Mongoose for modeling MongoDB objects within NodeJS.
- Support for the Sass CSS preprocessor.
Clone/Download the repo then edit
app.ts
inside/src/app/app.ts
# clone our repo
# --depth 1 removes all but one .git commit history
git clone --depth 1 https://github.com/datatypevoid/ng2-mean-webpack.git
# change directory to our repo
cd ng2-mean-webpack
# add required global libraries
npm install -g typings webpack-dev-server concurrently
# install the repo with npm
npm install
# create configuration json file for env vars
# save in `config/` as `config.json`
{
"ENV" : "development",
# Cannot be 8080 as this conflicts with our Webpack dev server
"PORT" : 3000,
"MONGO_URI" : {
"DEVELOPMENT" : "mongodb://[username:password]@host[:port]",
"PRODUCTION" : "mongodb://[username:password]@host[:port]",
"TEST" : "mongodb://[username:password]@host[:port]"
},
# Generate your own 256-bit WEP key here:
# http://randomkeygen.com/
# Note that you don't need to use specifically
# this, but it will certainly suffice
"SESSION_SECRET" : "355FC4FE9348639B4E4FED1B8E93C"
}
# build code
npm run build
# start up the stack
# this command runs two commands in parallel via `concurrently`:
# `npm run server` starts up `webpack-dev-server` allowing for
# hot module reloading
# `npm` run watch` uses `webpack` to watch the necessary files
# and build on file change
npm start
# in a separate terminal:
# start `Express` server
gulp serve
go to http://0.0.0.0:8080 or http://localhost:8080 in your browser
We use the component approach in our starter. This is the new standard for developing Angular apps and a great way to ensure maintainable code by encapsulation of our behavior logic. A component is basically a self contained app usually in a single file or a folder with each concern as a file: style, template, specs, e2e, and component class. Here's how it looks:
ng2-mean-webpack/
│
├──app/ * our source files for back-end routing and MongoDB object modelling
│ ├──models/ * model definitions for Mongoose
│ │ ├──user.model.js * a user model for use with PassportJS
│ ├──routes/ * store all modular REST API routes for Express here
│ │ └──authentication * an Express route for use with PassportJS
│ │ .router.js
│ └──routes.js * gather all of your Express routes and middleware here
│
├──config/ * configuration files for environment variables, Mongoose, and PassportJS
│ ├──config.json/ * allows definition of environment variables
│ ├──env.conf.js/ * contains utility functions for setting up environment vars
│ ├──mongoose.conf.js/ * configuration file for Mongoose
│ └──passport.conf.js/ * configuration file for PassportJS
│
├──sockets/ * directory for socket.io functionality
│ └──base.js/ * a basic socket.io server function
│
├──src/ * our source files that will be compiled to javascript
│ ├──main.ts * our entry file for our browser environment
│ │
│ ├──index.html * Index.html: where we generate our index page
│ │
│ ├──polyfills.ts * our polyfills file
│ │
│ ├──app/ * WebApp: folder
│ │ ├──todo/ * an example component directory
│ │ │ ├──todo.component.ts * a simple Angular 2 component
│ │ │ ├──todo.e2e.ts * simple test of components in todo.component.ts
│ │ │ ├──todo.spec.ts * a simple end-to-end test for /todo
│ │ │ ├──todo.html * template for our component
│ │ │ └──todo.service.ts * Angular 2 service linking to our API
│ │ ├──app.spec.ts * a simple test of components in app.ts
│ │ ├──app.e2e.ts * a simple end-to-end test for /
│ │ └──app.ts * App.ts: a simple version of our App component components
│ │
│ ├──assets/ * static assets are served here
│ │ ├──icon/ * our list of icons from www.favicon-generator.org
│ │ ├──service-worker.js * ignore this. Web App service worker that's not complete yet
│ │ ├──robots.txt * for search engines to crawl your website
│ │ └──human.txt * for humans to know who the developers are
│ │
│ └──sass/ * folder for Sass stylesheets
│ |
│ ├──base/
│ │ ├──_animations.scss * Animation keyframe definitions
│ │ ├──_reset.scss * Reset/normalize
│ │ ├──_typography.scss * Typography rules
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ ├──components/
│ │ ├──_buttons.scss * Buttons
│ │ ├──_carousel.scss * Carousel
│ │ ├──_cover.scss * Cover
│ │ ├──_dropdown.scss * Dropdown
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ ├─ layout/
│ │ ├──_navigation.scss * Navigation
│ │ ├──_grid.scss * Grid system
│ │ ├──_header.scss * Header
│ │ ├──_footer.scss * Footer
│ │ ├──_sidebar.scss * Sidebar
│ │ ├──_forms.scss * Forms
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ ├─ pages/
│ │ ├──_home.scss * Home specific styles
│ │ ├──_contact.scss * Contact specific styles
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ ├─ themes/
│ │ ├──_theme.scss * Default theme
│ │ ├──_admin.scss * Admin theme
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ ├─ utils/
│ │ ├──_variables.scss * Sass Variables
│ │ ├──_functions.scss * Sass Functions
│ │ ├──_mixins.scss * Sass Mixins
│ │ ├──_helpers.scss * Class & placeholders helpers
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ ├──vendors/
│ │ ├──_bootstrap.scss * Bootstrap
│ │ ├──_jquery-ui.scss * jQuery UI
│ │ ├──_module.scss * Load all partials from this directory into single partial
│ │ └── … * Etc.
│ │
│ │
│ └──main.scss * Main sass file to load all partials for this project
│
├──.babelrc * configure Babel 6 plugins and ES6/ES7 presets
│
├──server.js * ES5 `.js` file importing the server code along with a Babel 6 hook to transpile server ES6/ES7 code on the fly
├──server.conf.js * configure Express/Socket.io application, connect to database, instantiate Mongoose models, define API and front-end Angular routes, et cetera
│
├──gulpfile.js * ES5 `gulpfile` importing the `gulp` workflow code along with a Babel 6 hook to transpile the ES6 code on the fly
├──gulpfile.conf.js * contains all of the workflow management delegated to `gulp`: auto documentation generation; `sass` linting; `nodemon`, et cetera
│
├──spec-bundle.js * ignore this magic that sets up our angular 2 testing environment
├──karma.config.js * karma config for our unit tests
├──protractor.config.js * protractor config for our end-to-end tests
│
├──tsconfig.json * config that webpack uses for typescript
├──typings.json * our typings manager
├──package.json * what npm uses to manage it's dependencies
│
├──webpack.config.js * our development webpack config
├──webpack.test.config.js * our testing webpack config
└──webpack.prod.config.js * our production webpack config
What you need to run this app:
node
andnpm
(brew install node
)- Ensure you're running the latest versions Node
v4.1.x
+ and NPM2.14.x
+
Once you have those, you should install these globals with npm install --global
:
webpack
(npm install --global webpack
)webpack-dev-server
(npm install --global webpack-dev-server
)karma
(npm install --global karma-cli
)protractor
(npm install --global protractor
)typings
(npm install --global typings
)typescript
(npm install --global typescript
)
fork
this repoclone
your forknpm install
to install all dependenciestypings install
to install necessary typings- create
config.json
see below npm run build
to build necessary front-end code with Webpacknpm start
to enable hot module reloading and build on file change- In a new terminal,
node server
to start the server for the first time
The server.conf.js
file is expecting certain environment
variables
to be set within Node
. The env.conf.js
has functions to check whether the expected environment
variables
have been setup before proceeding to start up the rest of the server. You can create a file called config.json
and store it in the config
directory that looks something like this:
{
"ENV" : "development",
# MAKE SURE PORT IS NOT 8080 OR WHATEVER THE WEBPACK
# DEV SERVER PORT IS SET TO
"PORT" : 3000,
"MONGO_URI" : {
"DEVELOPMENT" : "mongodb://[username:password]@host[:port]",
"PRODUCTION" : "mongodb://[username:password]@host[:port]",
"TEST" : "mongodb://[username:password]@host[:port]"
},
# Generate your own 256-bit WEP key here:
# http://randomkeygen.com/
# Note that you don't need to use specifically
# this, but it will certainly suffice
"SESSION_SECRET" : "355FC4FE9348639B4E4FED1B8E93C"
}
After you have installed all dependencies and created your config.json
file, you can now run the app. First, you must start up the back-end server in a separate terminal using the gulp serve
command. This will fire up our Express app using nodemon
, which will watch for file changes and restart our backend when necessary. Next use the npm start
command in the original terminal which runs two npm
scripts in parallel: npm run server
to start webpack-dev-server
for building our front-end in the computer's memory, enabling hot module reloadig; npm run watch
to watch all of the front-end files and build them upon changes. You can now fire up your favorite web browser and visit the running application at localhost:8080
!
# development
# package front-end files with Webpack and hot reload
# upon any changes
npm start
# use `Gulp` in a second terminal to run the Express
# app responsible for our back-end
gulp serve
# optionally use `Gulp` in a third terminal to auto
# generate documentation and lint `Sass`
gulp
# production
npm run build:prod
npm run server:prod
gulp serve
gulp build:docs
gulp watch:docs
gulp watch:sass
# development
npm run build:dev
# production
npm run build:prod
npm run watch
npm run test
npm run watch:test
# make sure you have your server running in another terminal
npm run e2e
npm run webdriver:update
npm run webdriver:start
npm run webdriver:start
# in another terminal
npm run e2e:live
You can include more examples as components but they must introduce a new concept such as Home
component (separate folders), and Todo (services). I'll accept pretty much everything so feel free to open a Pull-Request
To take full advantage of TypeScript with autocomplete you would have to install it globally and use an editor with the correct TypeScript plugins.
TypeScript 1.7.x includes everything you need. Make sure to upgrade, even if you installed TypeScript previously.
npm install --global typescript
We have good experience using these editors:
- Visual Studio Code
- Webstorm 10
- Atom with TypeScript plugin
- Sublime Text with Typescript-Sublime-Plugin
When you include a module that doesn't include Type Definitions inside of the module you need to include external Type Definitions with Typings
npm install --global typings
When including 3rd party modules you also need to include the type definition for the module if they don't provide one within the module. You can try to install it with typings
typings install node --save
If you can't find the type definition in the registry we can make an ambient definition in this file for now. For example
declare module "my-module" {
export function doesSomething(value: string): string;
}
If you're prototyping and you will fix the types later you can also declare it as type any
declare var assert: any;
If you're importing a module that uses Node.js modules which are CommonJS you need to import as
import * as _ from 'lodash';
You can include your type definitions in this file until you create one for the typings registry see typings/registry
-
What's the current browser support for Angular 2 Beta?
- Please view the updated list of browser support for Angular 2
-
Why is my service, aka provider, is not injecting parameter correctly?
- Please use
@Injectable()
for your service for typescript to correctly attach the metadata (this is a TypeScript problem)
- Please use
-
How do I run protractor with node 0.12.x?
- please check out this repo to use the old version of protractor #146
-
Where do I write my tests?
- You can write your tests next to your component files. See
/src/app/home/home.spec.ts
- You can write your tests next to your component files. See
-
How do I start the app when I get
EACCES
andEADDRINUSE
errors?- The
EADDRINUSE
error means the port8080
is currently being used andEACCES
is lack of permission for webpack to build files to./dist/
- The
-
How to use
sass
for css?loaders: ['raw-loader','sass-loader']
and@Component({ styles: [ require('./filename.scss') ] })
see issue #136 from the Angular 2 Webpack Starter Kit
-
How do I test a Service?
- See issue #130
-
How do I add
vscode-chrome-debug
support?- The VS Code chrome debug extension support can be done via
launch.json
see issue #144 from the Angular 2 Webpack Starter Kit
- The VS Code chrome debug extension support can be done via
-
How do I make the repo work in a virtual machine?
- You need to use
0.0.0.0
so revert these changes #205 from the Angular 2 Webpack Starter Kit
- You need to use
-
What are the naming conventions for Angular 2?
- please see issue #185 and PR 196 from the Angular 2 Webpack Starter Kit
-
How do I include bootstrap or jQuery?
- please see issue #215 and #214 from the Angular 2 Webpack Starter Kit
-
I'm getting an error about not finding my module that I installed?
- please see How to include or create custom type definitions and custom-typings.d.ts from the Angular 2 Webpack Starter Kit
-
How do I async load a component?
- { path: '/about', loader: () => require('es6-promise!./about/about')('About') }
- Also see es6-promise-loader
AngularClass for their Angular 2 Webpack repo which served as a starting point for the front-end
Contact us anytime for anything about this repo, Angular 2, or MEAN stack development in general.
enjoy -- Da5id
Looking for corporate Angular/MEAN training, want to host us, or Angular/MEAN consulting? david.r.niciforovic@gmail.com