kilianc / node-apiserver Goto Github PK
View Code? Open in Web Editor NEWA ready to go, modular, multi transport, streaming friendly, JSON(P) API Server.
Home Page: http://kilianc.github.com/node-apiserver/
License: MIT License
A ready to go, modular, multi transport, streaming friendly, JSON(P) API Server.
Home Page: http://kilianc.github.com/node-apiserver/
License: MIT License
add auto compression when accepted from the browser
add websocket transport
something like:
http://swagger.wordnik.com/
Hi!
How to get the bare response object in the module? Hence I can do something like:
var request = require('request')
request.get('http://mysite.com/doodle.png').pipe(resp)
Do we need to change the apiserver.js file to make the server run in cluster mode to handle high throughput?
also is the agent limit set to infinity
globalAgent.maxSockets = Infinity
We should ad a parameter to responseStreamStart
specifying the kind of input or the start/end stream strings.
The main goal is to make something like this possible:
MyApiModule.prototype.testPublic = function(request, response) {
var self = this;
// start/stop strings
self.emit("responseStreamStart", request, response, 200, null, ['{', '}']);
// or json type defined in apimodule.js
self.emit("responseStreamStart", request, response, 200, null, ApiModule.responseStreamTypes.OBJECT);
var myAsyncEventEmitter = new SomethingAsync();
myAsyncEventEmitter.on('data', function(err, item){
self.emit("responseStreamData", request, response, { key: randomKey(), value: item }, false);
}
myAsyncEventEmitter.on('end', function(err, item){
// last object (avoiding trailing commas)
self.emit("responseStreamData", request, response, { key: randomKey(), item: item }, true);
self.emit("responseStreamEnd", request, response);
}
}
this should produce a response like this
{
"random1": { /* item content */ },
"random2": { /* item content */ },
"random3": { /* item content */ },
"randomN": { /* item content */ },
...
}
I was wondering if this supports the PATCH verb. If not, is it possible for me to override that method using some other way?
Can we define an endpoint to handle all verbs? and define non-mandatory params as in Express?
for example:
server.all('/svc/:id/:connectionType?', function(req, res){
})
Where all means we will handle all verbs while ?
defines non-mandatory param of connectionType
Thanks!
Trace below. This happens because the domain option is copied to the ApiServer instance. In 0.8.x EventEmitter tries to 'enter' the domain. I changed the name of the domain option to 'domain_compat' and to get it working.
The real fix? Use a sub-object for the EventEmitter instead of inheriting it? Namespace the options? Both inconvenient.
events.js:80
this.domain.enter();
^
TypeError: Object localhost has no method 'enter'
at EventEmitter.emit (events.js:80:19)
at onRequest (.... /node-apiserver/examples/account/node_modules/apiserver/lib/apiserver.js:120:8)
at Server.EventEmitter.emit (events.js:91:17)
at HTTPParser.parser.onIncoming (http.js:1783:12)
at HTTPParser.parserOnHeadersComplete as onHeadersComplete
at Socket.socket.ondata (http.js:1680:22)
at TCP.onread (net.js:410:27)
If any of the request is getting timeout we are getting 'Can't set headers after they are sent' error when we are returning response. To return response we are using return response.serveJSON() method . Is api server already returning response in case timeout? How to handle timeout?
on node v0.8.14, receive this error when running the account example.
console-trace/console-trace.js:43
arguments[0] = console.traceFormat(__stack[1], name) + pad + arguments[0]
ReferenceError: __stack is not defined
at Object.forEach.console.(anonymous function) as info
at Server.mongodbDb.open.ApiServer.timeout (/Users/username/Code/node_projects/node-apiserver/examples/account/server.js:25:13)
at Server.g (events.js:192:14)
at Server.EventEmitter.emit (events.js:93:17)
at Server._listen2 (net.js:922:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Apiserver streamResponseData
prototype:
ApiServer.prototype.streamResponseData = function(sender, request, response, responseChunk, isLast)
the server should check a missing isLast == true responseStreamData event ading a dummy empty object,
or responding with an error.
self.emit("responseStreamData", request, response, obj1, false);
self.emit("responseStreamEnd", request, response); //error
It should also check a double isLast == true responseStreamData event.
self.emit("responseStreamData", request, response, obj1, true);
self.emit("responseStreamData", request, response, obj2, true); //error
Hi,
I'm working on a Node API-server to create (what I'd like to call a) "Utils as a Service', as the main application is in C# and some features are easily achieved in Node. Currently, I'm working on a utility that gets html+js as import and DOM-rendered html (or image/pdf) as output.
The thing is, I'm having trouble finding out how to let my API-endpoint use POST-data, instead of GET-data or url-parsed data. In my case, input parameters as GET-variables aren't such a good idea.
I have the following setup:
Router
["/DOMrenderHtml/:html/:outputType", "v1/renderer#render"]
API module
/**
* Renderer api-module
*/
var Renderer = module.exports = function (options) {
var self = this
options = (options !== null && options !== undefined && options.constructor === Object) ? options : {}
Object.keys(options).forEach(function (key) {
if (!self.__proto__.hasOwnProperty(key)) {
self[key] = options[key]
}
})
};
/**
* function: render
*/
Renderer.prototype.render = function (request, response) {
//get parameters
var html = request.querystring.html;
var outputType = request.querystring.outputType;
What works:
curl http://localhost:3000/DOMrenderHtml/some-data/pdf/
Result::
request.querystring: { html: 'some-data', outputType: 'pdf' }
What I'm trying to do:
curl -X POST http://localhost:3000/DOMrenderHtml/ -d "{"html": "some-data", "outputType:"pdf"}" -H "Content-type: application/json"
Result::
request.querystring: { }
Nor is their any trace of my POST-data in the request-variable.
Conclusion:
What am I missing here? Couldn't find any example on how the actual API-call is expected to be made, except:
Server will respond to
GET, POST http://localhost:8080/foo
GET, POST http://localhost:8080/foo/5/true
GET, POST http://localhost:8080/foo_verbose/5
* http://localhost:8080/bar
* http://localhost:8080/1/foo_module/bar
Does this mean POST-data is not supported at all?
I would be very grateful for any help you are able to provide:)
This was an issue I experiences with both the example script and my own scripts.
When trying to parse the body buffer on a post request, the event listeners for .on('data' and .once('end' would not trigger.
I came up with a fix by wiping out lines 6 and 104 on apiserver.js disabling buffered-request module as node http now handles these events by default.
You may see my fork at https://github.com/skliffmueller/node-apiserver
Ex:
/users/:id/photo
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.