Traffic flow Restful API

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

What is restful?

RESTful, is one of the most popular Internet software architectures at present. Because of its clear structure, standards, easy to understand, easy to expand, so more and more sites are being adopted. Learning restful is the first thing to learn about rest.

REST

  • Resources rest is "representation State transformation", in fact, rest omits the subject. "Presentation layer" actually refers to "the expression layer" of "resources". A picture, a document, a video, and so on. These resources are located by URI, which means that a URI represents a resource.

  • Presentation layer (representation)
    Resource is to make a concrete entity information, he can have a variety of presentation. For example, a TXT text message, he can be exported to HTML, JSON, XML and other formats, a picture he can be jpg, PNG and other ways to show
    URI determines a resource, but how does it determine its specific manifestation? It should be specified in the header information of the HTTP request with the Accept and Content-type fields, which is the description of the "presentation layer".

  • State conversion (Transfer)
    Accessing a Web site represents an interactive process between the client and the server. In this process, it is certain that data and state changes are involved. While the HTTP protocol is stateless, these states are definitely stored on the server side, so if the client wants to notify the server of changes to the data and state, it must be notified by HTTP. Specifically, there are four verbs in the HTTP protocol that represent the mode of operation: GET, POST, PUT, DELETE. They correspond to four basic operations: get is used to get a resource, post is used to create a new resource (and can also be used to update resources), put is used to update resources, and delete is used to delete resources.

    RESTful API

    A restful API is called the Web API that fits the rest design style. That is, restful is an application of rest architecture style on the Web

    Implementation features

  • Access to all traffic data
    GET: /api/<Authen>/all

  • Access to nearly a week of traffic data
    GET: /api/<Authen>/near

  • Access to a specified junction (CROSSID) that specifies the gateway all traffic flow data
    GET: /api/<Authen>/cross/<crossID>

  • Access to a designated junction (CROSSID) that specifies the gateway for nearly 3 minutes of real-time traffic data
    GET: /api/<Authen>/cross/<crossID>/last3min

  • Access to a specified junction (CROSSID) that specifies the gateway for a period of time traffic data
    GET: /api/<Authen>/cross/<crossID>/start=<startTime>&end=<endTime>

    Code section

  • Database Section Flow.js
    Building a schema: creating

    var FlowSchema = new Schema({ CrossTrafficData:{     CrossID: String,     DeviceType: String,     DateTime: String,     Interval: Number,     DataList:{         Data:{             LaneNo: String,             Volume: Number,             AvgOccupancy: Number,             AvgHeadTime: Number,             AvgLength: Number,             AvgSpeed: Number,             Saturation: Number,             Density: Number,             Pcu: Number,             AveQueueLength: Number,             Volume1: Number,             Volume2: Number,             Volume3: Number,             Volume4: Number,             Volume5: Number         }     } }   });

    Exports object:
    var FlowOp = function(){};
    Define various functions. Prototype indicates that it is used for FLOWOP objects and returns data by callback
    FlowOp.prototype.<name_function> = function([parametres],callback){}
    Define a model object to facilitate the use of the model encapsulation method
    var FlowModel = mongodb.mongoose.model('flowCar',FlowSchema);
    Model object's Find method, parameters: condition, projection, CB
    The following sentence indicates that a file is filtered out of the parameter ID value and time period, and the _id and __v properties are removed after extraction. The query results are returned to data_id_p, which goes into the callback function

    FlowModel.find({'CrossTrafficData.CrossID': id,'CrossTrafficData.DateTime': {"$gt": start, "$lte": end}},'-_id -__v',(err,data_id_p)=>{});

    A callback function that converts the JS obj to JSON, then assigns a value to the callback, passing the JSON value

    var data_p_json= JSON.stringify({data_p});callback(err,data_p_json);
  • The
  • Route routedata.js
    //json to XML, with the help of the Jsontoxml package and the XML declaration, the previously passed JSON is obj

      router.get ('/cross/:id/ Start=:timestart&end=:timeend ', (req, res) = {Carflow.findbycrossbyperiod (req.params.id,     Req.params.timestart, Req.params.timeend, (err, obj) = = {var data_xml = jsontoxml (obj);     Res.set (' Content-type ', ' text/xml ');     var d = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?>"; Res.send (d + data_xml); });});

    Rethinking

    The

    can establish a function in the schema and export a model object for better encapsulation?

      MySchema.methods.findByCByP = function (paras, CB) {return this.find ({Type:this.type}, CB);}  

    Reference

    Rest:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/08.3.md

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.