Skip to content

Instantly share code, notes, and snippets.

@diggzhang
Created June 23, 2016 10:08
Show Gist options
  • Save diggzhang/359f41c53394c61ac9ebe8fea3877fb3 to your computer and use it in GitHub Desktop.
Save diggzhang/359f41c53394c61ac9ebe8fea3877fb3 to your computer and use it in GitHub Desktop.
一个promise的request
"use strict";
import mongoose from 'mongoose';
const EventAuto = mongoose.model('EventAuto');
const autoEventsProxy = "http://localhost:8080/v3_6/autoevents/";
const request = require('request').defaults({
json: true
});
function requestP(url, method, header, body) {
return new Promise(function (resolve, reject) {
request({
method: method,
url: url,
body: body,
headers: header
}, function(error, httpResponse, body) {
if (error) {
console.error(url + " : " + error);
} else if (httpResponse.statusCode !== 204) {
reject(new Error(body.message));
} else {
resolve(body);
}
});
});
}
class EventAutoController {
constructor(attributes) {
// this.attributes = clone(attributes || {})
};
static *save(eventsHeader, events) {
delete eventsHeader.header['content-length'];
var reqConfig = {
headers: eventsHeader.headers,
url: "http://localhost:8080/v3_6/autoevents",
method: 'POST',
body: events
};
yield requestP(reqConfig.url, reqConfig.method, reqConfig.headers, reqConfig.body);
try {
EventAuto.create(events);
} catch (e) {
console.error(e);
console.error(events);
}
};
}
module.exports = EventAutoController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment