auto generate Rest service in node.js

2019-07-03 23:56发布

问题:

Is there a way to auto generate a Rest service in node.js like in rails? I just want to expose my mongodb models as a Restfull service. I am now doing a lot of repetitive work with Mongoose and express-resource.

回答1:

Yep, it's possible, follow this commands:

npm install railway
railway init rest-app && cd rest-app && npm install
railway generate scaffold resource property1 property2
railway server 3000

That's it! If you need to generate nested resource:

railway generate resource.nestedresource propertyForNestedResource

and modify config/routes.js:

map.resources('resource', function (res) {
    res.resources('nestedresources');
});

Check this example app: https://github.com/anatoliychakkaev/railway-example-app

Welcome to railway: http://railwayjs.com



回答2:

You can also use a library I developed if Railway is too bulky for your needs:

https://github.com/enyo/mongo-rest

It's a simple library, well tested, that just provides a REST interface for your mongoose models.