Old web New branch----sails.js new productivity of mobile devices (ii)

Source: Internet
Author: User

Select Sails.js

An obvious node. js style. Specifically, Sails uses layered development and conventions over configurations (Convention over configuration), two development techniques that have been popularized by Rails and similar frameworks. Sails also integrates some other node. JS Packages as key components, which means that you do not need to learn the framework from scratch. Another advantage is the fact that Sails.js's documentation is constantly improving, which is a key indicator of any open source project to look for.

In the first part, we focus on how Sails can achieve layered development. The next few sections will introduce the conventions better than the other conveniences of configuring and using Sails encoding.

New MVC

" This reconfiguration of the legacy  MVC  architecture enables architects and developers to find what they've been looking for in the application server for a long time: a single location with business logic and validation, and a common interface for multiple clients. "

It is different from transferring data (usually in JSON or XML format) to the client that is transferred back to Html,sails by the server. The client (a single-page Web application written by a mobile application Avascript framework) then uses that data and renders it in the way that best fits the client. For most clients, the data presentation format will be JSON, because many single-page WEB and mobile clients are written in JavaScript and can be easily used with JSON. And if necessary, it is not difficult to use different data representation formats like XML.

This reconfiguration of the traditional MVC architecture implements what architects and developers have long looked for in the application server: a location that contains business logic and validation, and a common interface for multiple clients. The server only needs to be a presentation design data element that can be identified by the client, which provides an easy way to perform traditional business logic and data validation over an HTTP endpoint.

HTTP API Sample Application

This new architecture (I call it the HTTP API) , However, the HTTP API system abandons some of the flexibility and "common client" functionality provided by REST in exchange for a simpler model.

Unlike REST systems, HTTP API clients require some inferred knowledge of the systems they communicate with. They need to know the expected results and the available endpoints in advance. This is the opposite of the REST architecture, but I find the simplicity it is worth.

In an HTTP API, all communication between the client and the server occurs on HTTP. For the sample application, I will use JSON as the data format for the interchange, using standard HTTP insight to represent the operation (GET for read, POST for INSERT, PUT for UPDATE, delete for deletion). I will also use the HTTP header as a mechanism for conveying out-of-band information, such as authentication data. However, some state data is stored on the server side, and the data exchanged is all JSON and is assumed to be specific to this system.

Consumer samples for the HTTP API-Mobile applications, single-page JavaScript applications, or even other servers that want to access the data contained here-will know the type of JSON I will send back to them. Demonstrates that any application should be able to "discover" the information sent back. More importantly, the consumer will know the subsequent HTTP endpoints that their data will be sent to, retrieved, or modified.

Required Preparation: Prerequisite software and Settings

"Native" uses HTTP to submit a GET request, POST request, and so on. You should not be intimidated by the idea of viewing dynamic HTTP requests.

  but the fact that You only need the practical knowledge of  JavaScript  to get started with  sails.

for the development environment, make sure that node. js on your machine is up-to-date and has the latest NPM version installed . Although the node. JS Community is obviously trying to keep versions backwards compatible, there is no guarantee that the code I write here will work for earlier versions of the technology. shall not be liable for sale.

installing sails.js and MongoDB

The only installation you need to use Sails.js is to install it to the NPM cache globally, just like this:

npm install-g sails.js .

When you are finished, you will have a utility commandSailsavailable for use. Like NPM,Sailsis a command-line utility written for node. js using JavaScript. It acts as a single-source utility that performs a variety of operations using the Sails.js framework-such as creating a new application, running the application, running the application in a debug loop, generating a new component for the application, and so on. When installing sails.js with NPM, be sure to use- Goptions. Otherwise, it will only be installed in the current project directory, and this is one of the few things that the utility really needs to be global.

While waiting for the sails.js framework to download and install, take a moment to make sure that you have MongoDB set up in your development environment. If not, it's a good time to download a setup program that fits your target platform .

Note that Sails.js uses more than a single MongoDB backend; in fact, Sails.js uses a disk-based lightweight format out-of-the-box. This is not a recommended setting for production purposes, but it is well suited for examples like ours. It will take a while to get to the part of this series that uses MongoDB, but you must be happy to have it when I use it.

Check the installation

sails.js complete the installation? Use the command sails version to verify it.

As of this writing. As long as the output generated by the above command is not an error, it indicates that Sails is installed and ready to use.

sails.js Display "Hello, world!"

By convention, the first thing a programmer does when learning a new language or environment is to write the simplest program in that language: a Hello world application. Fortunately, Sails generates a Hello world as the basic application framework when you create a new application. I can use this application to confirm that the Sails infrastructure is valid. In homage to the Agile development Strategy,

Create a basic sails.js application

--no-frontend

--no-frontend

from the development machine you want the directory where the code resides, by Sails The command line emits a New command, and then name your application:

Sails new Hello-sails .

After completing this step, Sails will wait a moment and then reply to a message like this: "Created a new Sails app ' Hello-sails '!" (If you return any additional information, there may be an error in your sails.js installation somewhere.) ) in the target directory (inferred from the application name), you will see that sails.js generates many directories and files.

In general, it is safe to assume that there is no problem creating a framework (as long as no one touches it), but it can be helpful to know how to run the application locally. Start our operation with a "sails lift" .

Figure 1: Running the sails.js locally

650) this.width=650; "Width=" 830 "height=" 698 "src=" http://s3.51cto.com/wyfs02/M00/89/B6/ Wkiol1gav73dzkpmaamwxtxkf6s265.png "style=" Background:url ("/e/u261/lang/zh-cn/images/localimage.png") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "title=" capture. PNG "/>

It is also an impressive display of ASCII works. More importantly, this small ASCII image confirms that node. JS is successfully running the application we built on port 1337 on the local machine. By accessing it in the browser, a default Sails.js Web page appears with links to the main Sails home page and documents.


This article is from "Swiss departure-Next generation Enterprise Application" blog, declined reprint!

Old web New branch----sails.js new productivity of mobile devices (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.