Эксперты сообщества

Спрашивайте и учитесь у фрилансеров-экспертов

7 ответов(-а)

Greg Bogdan
Greg Bogdan
ответил 2 years ago
Нанять меня

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 отметка(-ок) "Нравится"
drudev
drudev
ответил 1 year ago
Нанять меня

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

1 отметка(-ок) "Нравится"
pashute
pashute
ответил 1 month ago
Нанять меня

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 отметка(-ок) "Нравится"
markasoftware
markasoftware
ответил 1 year ago
Нанять меня

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 отметка(-ок) "Нравится"
Nayana Hettiarachchi

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 отметка(-ок) "Нравится"
carlosgottberg
carlosgottberg
ответил 2 years ago
Нанять меня

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 отметка(-ок) "Нравится"