Introduction
Recently, in the angular community of prototype developers, a full JavaScript development architecture mean is suddenly popular. The first letter represents: (M) Ongodb--nosql document database, using JSON style to store data, and even use JS for SQL query; (E) xpress--node-based web development framework; (A) agular-- JS's front-end development framework provides declarative bidirectional data binding, and (N) ode--based on the V8 Runtime environment (JS language development) to build fast-response, scalable network applications.
Mean's supporters claim that if JS is used throughout the development stack, there is no doubt that it is a big selling point to improve efficiency significantly. As a result, developers, both front-end and back-end, can not only use a consistent data model, but also have a consistent programming experience in other areas.
For example, take MONGO, you can use the class JSON format (BSON, binary JSON) to store the data, then call the JSON query statement in Express/node, and then pass the result in JSON format to the front-end angular display, so that It also naturally makes the debug program much easier.
Note: In fact, in the mean architecture, the front-end angular is not required, and you can replace it with other front-end frames, such as backbone, Ember, or polymer.
Why Choose MongoDB?
As mentioned above, the most important advantage of this architecture is the ability to use a single language, which is the primary reason for its choice of MONGO. It's not about NoSQL. Some people have blamed the mean architecture for the fact that MongoDB is well qualified for small and medium-sized applications, but may be stretched for large-scale applications (millions users). What I want to say is that it all depends on how you use it.
The SQL database itself is strongly typed, so it is possible to maintain a level of consistency to a large extent, ensuring that many types of dirty data are not able to enter the database at the outset. And NoSQL is a weak type of database, which makes it inadequate in data validation, but only for developers to implement, based on this feature, it is particularly suitable for storing those non-canonical data, especially in the prototype development phase, the data model is undergoing rapid changes.
The technical difference between SQL and NoSQL boils down to balancing performance and stability. In some cases, transaction processing of data will not change easily once it is set up, so it is very appropriate to use MONGO at this time, but sometimes it involves more complex transaction processing, involving many independent business logic, because MONGO does not provide a simple data model to support a certain level of atomic operations , so SQL can come in handy at this time.
But anyway, whether or not you choose M in mean, you ultimately need to choose the right tool to do the right thing according to your own needs.
Why Express?
You can simply think of Express as a set of tools to build a Web application under the node platform. On the basis of node, it provides a number of simple interfaces to create request nodes, process cookies, etc., and also provides many features to help you build your own server. In general, Express has advantages in the following areas:
- To set up rest routing simple emitter:
1 |
app.get(/account/:id, function(req, res){ /* req.params(‘id‘) is available */ });
|
- Support for template engines such as jade or mustache
- Automatic HTTP Header processing:
1 |
app.get(‘/‘, function(req,res){ res.json({object: ‘foo‘}); });
|
- Supports connect middleware, which can insert additional request or response processing, such as user authentication
- Provide auxiliary function to parse POST request
- Prevent XSS
- Elegant error handling
How to get started quickly mean
If you want to get started quickly mean, then Mean.io is a good choice. The project is designed to address some of the common integration issues in the mean architecture, is well maintained, the documentation is clear, and it is easy to add a third-party library on your own, as well as with Yeoman (via Generator-mean by James Cryer).
Before further reading, assume that we agree to the following: (a) MONGO is at least well suited to the prototype design of the all-JavaScript stack; (b) admits that even as angular, one day will be replaced by some other JS frameworks, As long as they can help us quickly and easily build this architecture up.
The next step is to introduce Yeoman, which consists of 3 familiar tools:
- Grunt: for building, previewing and automating your project, this is especially thanks to the many Grunt tasks created by the Yeoman and grin T-contrib teams.
- Bower: Front-end dependency management tool, with it you no longer need to manually download and manage the third-party JS library.
- YO: Quickly build a new application, including configuring your grunt tasks and the Bower dependencies you will most likely use.
A year ago, I had created a project called Expressstack with some other people, and the idea was simple to provide tools to quickly generate everything needed to build a full-JS Web application. However, the project was aborted, but many similar projects have sprung up.
Some of these items are described below:
Note: You may need to install Yeomam (npm install-g yo) and some of the following generators (NPM install-g <generator-name>).
Generator-angular-fullstack
This is a ANGULARJS generator, integrated with Express, optional MongoDB. The main functions are as follows:
- Support for client and server livereload.
- Express Server integrates the grunt tasks.
- A convenient deployment process is built in.
- Support Jade.
Refer to: http://tylerhenkel.com/creating-apps-with-angular-and-node-using-yeoman/
Generator-meanstack
Another generator for the mean architecture, integrated with grunt-express, functions as follows:
- On the basis of Generator-angular, Express replaced Connect.
- Support for client and server livereload.
- Use the App_grunt.js file to start the app, and define the route in App.js.
- The catalogue structure basically inherits the style of Generator-angular, only made a little change.
Refer to: Https://github.com/Grievoushead/generator-express-angular
Generator-mean-seed
Integrated with Mongo,express,angular,yeoman,karma and protractor (for automated testing).
Generator-klei
Similar to the others, but using mongoose and stylus, other features include:
- Its directory structure is very easy to expand (contains a TODO list application example)
- A fully configured Gruntfile, Livereload, linting, concatenation, minification, etc. are assembled.
- Use Exctrl to move from the Mount API.
- Using Grunt-injector, you can automatically load newly added JS and CSS without having to manually modify the HTML layout file.
- Use Karma, mocha, and Chai to perform unit tests on the front end.
Ultimate-seed-generator
The generator is very comprehensive, adding many third-party libraries, including passport for user login, browserify loading JS.
- Integrated Angularui, barbeque (for task queue management) and bootstrap
- Integrated with Bower, browserify,express and Font Awesome
- Integrated with grunt, handlebars, JQuery, Jshint and Karma/mocha
- Supports Less/lesshat, Livereload and Lodash/underscore
- Integrates with Modernizr, mongodb/mongoose and passport
How to make a choice?
Having seen so many generators, naturally ask, which one should I choose? In fact, the list above is sequential, based on its compatibility with the latest version of the Yeoman and its maintenance activity.
Is the all-JavaScript architecture suitable for product-level applications?
Admittedly, it would be great to be able to use JavaScript at every level of the development stack (at least for prototyping), but be careful not to tie yourself down in pursuit of this goal. While it is true that a growing number of large-scale applications are adopting similar architectures, such as Walmart, LinkedIn, it does not mean that they will succeed if they imitate them.
Another important note is that it is much more difficult to build the backend on node than in other languages such as Ruby,python or Java. You might want to handle memory leaks yourself, avoid time-consuming operations in the event loop, and be very careful with exception handling, which can cause the entire application server to crash if improperly handled, but these issues are well handled on other platforms. However, this is not to say that node can not be used in the production environment, of course, but with extra effort.
To be honest, it would be very difficult to provide a chatty solution for a Web application, and the mean architecture must have its limitations. There's a lot of evolution in the design patterns, principles, and styles of the front-end and back-end, and if you think PHP or rails is a smarter choice, keep using it, or try mean, at least in the present.
"Original: http://addyosmani.com/blog/full-stack-javascript-with-mean-and-yeoman/"
"Go" All-JavaScript Web development architecture: Mean and Yeoman "translation"