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
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);
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