Gli esperti della community

Fai delle domande e impara dai freelance più esperti

10 Risposte

Greg Bogdan
Greg Bogdan
ha risposto Jan 7, 2016
Assumimi

Hi moemoezz,

Well thats a good question. One of the most widely used frameworks is express.js, but it all depends on your API and if you want to build anything else, I mean besides API you want to add 1-2 pages.

Another option is restify.js, its an old player on the field and is very similar to express, except it does not have template support. If you only want REST API in your app, then restify should be a good selection

10 mi piace
drudev
drudev
ha risposto 7 anni fa
Assumimi

If you're new to Node or its web frameworks, I'd just stick to Express for now. It is by far the most popular framework and has tons of resources online. It may not specialize in REST webapps like some of the other frameworks, but it still does a great job.

To get started, all you really need is a few lines of code like this:

var express = require('express');
var app = express();

app.get('/hello', function(req, res) {
res.send({
message: 'world'
});
});

var server = app.listen(8080);

2 mi piace
Prasad wandre
Prasad wandre
ha risposto 4 anni fa
Assumimi

LoopBack is a highly-extensible Node. js framework that enables you to create dynamic end-to-end REST APIs with little or no coding. It is designed to enable developers to easily set up models and create REST APIs in a matter of minutes. It supports easy authentication and authorization setup.

0 mi piace
webNeat
webNeat
ha risposto 5 anni fa
Assumimi

The most used NodeJS framework is ExpressJS, but it does only provide basic functionalities. That's why I have built a custom framework based on it and Mongoose to make building REST APIs much easier: https://github.com/wajez/api

0 mi piace
pashute
pashute
ha risposto 5 anni fa
Assumimi

Express.js is a little old but its the most popular. Koa.js is newer, written by Express.js developers and uses generator.js model. Hapi.js is more readable. Here are some examples:

Here's a tutorial for express.js: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4

The express.js code looks like this:

// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080; // set our port

// ROUTES
var router = express.Router(); // get an instance of the express Router

// root rout
router.get('/', function(req, res) { // accessed at GET http://localhost:8080/api)
res.json({ message: 'api works!' });
});

// more routes
router.route('/bears')

// Create car. POST http://localhost:8080/api/car)
.post(function(req, res) {
var car = new Car(); // class defined elswhere and mongoose configured. See tutorial
car.name = req.body.name;
car.save(function(err) {
if (err) res.send(err);
res.json({ message: 'Car created!' });
});
});
// ... similar for .delete .get and .update

// Rout defaults ("registration": prefix all with api)
app.use('/api', router);

// Server started here
app.listen(port);
console.log('Server started on port ' + port);

A tutorial for REST in Koa.js can be found here https://mherman.org/blog/2017/08/23/building-a-restful-api-with-koa-and-postgres/

Code is actually simple although the tutorial is explaining the tools that create automatic database access and models for you and includes testing tools to test your code even before you started writing it (Test Driven Development)

Here's a sample router section of the code:

router.get(BASE_URL, async (ctx) => {
try {
const cars = await queries.getAllCars();
ctx.body = {
status: 'success',
data: movies
};
} catch (err) {
console.log(err)
}
})

0 mi piace
markasoftware
markasoftware
ha risposto 7 anni fa
Assumimi

Everybody here seems to recommend Express, and it's definitely the most popular, but there definitely are alternatives worth taking a look at. Koa is another one, which is actually developed by Express developers. It is similar to Express in many ways, and uses the new "generators" feature in javascript to handle asynchronous code in a very interesting (and arguably better) way instead of just using callbacks. It also makes writing middleware easier because of the nature of generator functions. One more framework to use is Hapi, which has a much more verbose syntax than Express and usually results in longer code. This can be seen as a waste of time to write, but on the other hand it produces code that is arguably more readable than Express's.

0 mi piace
Ruslan Kyba
Ruslan Kyba
ha risposto 7 anni fa
Assumimi

Choose tool with the biggest community. So it is ExpressJS! You can find a lot of guides and tutorials for it how to build Restful API with it.

0 mi piace
Nayana Hettiarachchi
Nayana Hettiarachchi
ha risposto Apr 29, 2016
Assumimi

to find the best framework you have to try few of them and see what works best for you. the word "best" is personal. what works best for me may not work best for you. and "best" can have different meaning for different people.
for example i am happy with using just connect JS and keeping it light weight. Some prefer to bring a swagger based framework to provide a better UI for those who want to play with the API. Some can be hardcore and just build their rest stuff using just plain http. Hope this helps.

0 mi piace
carlosgottberg
carlosgottberg
ha risposto Mar 13, 2016
Assumimi

I think ExpressJS is kind of a popular choice for it. That being said, there's probably no 'best framework'. All of them will have their share of advantages and disadvantages. You take what you feel comfortable with and be done with it.

It is generally a good sign if the framework follows common best practices and principles in software and that is easy to debug (if following best practices, this should not be a problem, should it ;) ).

@Techly: You might want to check Lumen (a micro-laravel) or Slim. Lately I've found myself spending a lot of time on PHP and... well, not my favorite language, you can be sure. The above paragraph applies to this as well.

0 mi piace