Coder Social home page Coder Social logo

kilianc / node-apiserver Goto Github PK

View Code? Open in Web Editor NEW
224.0 12.0 24.0 895 KB

A ready to go, modular, multi transport, streaming friendly, JSON(P) API Server.

Home Page: http://kilianc.github.com/node-apiserver/

License: MIT License

JavaScript 99.86% CSS 0.14%

node-apiserver's People

Contributors

cirpo avatar g3z avatar kilianc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-apiserver's Issues

how to make the API server run in cluster mode

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

streaming api full json comaptible

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 */ },
    ...
}

Support PATCH?

I was wondering if this supports the PATCH verb. If not, is it possible for me to override that method using some other way?

server.all and non-mandatory param

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!

account example error on node 0.8.4

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)

Can't set headers after they are sent

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?

ReferenceError: __stack is not defined

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)

streaming response should check and fix trailing/missing commas

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

API-endpoint not using POST-data

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.