2

I've started working on a new application where the backend will be nothing but a REST API. I'm fairly new to Node, so I started with Express. I'm still in the very early stages, so I'm looking for suggestions on the right framework to use.

I've looked at Express, Koa, HAPI, LoopBack and Restify. I like Restify but it doesn't seem like the most popular option out there. I also like the looks of LoopBack.

I'd love to use Swagger / OpenAPI in some form to document my API. I've seen a number of modules for Express that have also been modified by others to work with Restify.

Any suggestions? Thanks!

3 Answers 3

1

There is a ton of support for Express, but you can pretty much mash together all of the things that you like to use. I like to use the node-mysql node module so that I can use mysql on the backend. I honestly just create my own APIs. They are really easy to build. You just define your routes in a js file and then define your get, post and delete functions for each endpoint. I hope that helps.

EDIT: Here is a tutorial for making a REST API with NodeJS, Express and MySQL. This will get you up and going in less than a few hours if you know what you are doing with javascript.

http://www.yogasaikrishna.com/simple-restful-api-using-nodejs-express-and-mysql/

Sign up to request clarification or add additional context in comments.

Comments

1

I would keep going with express especially if you're new to Node. It has lots of documentation, is incredibly flexible, and is actively maintained. Using express makes it easy to take advantage of CRUD and respond to and with JSON. It includes a router, this for example makes it easier to prefix routes with something such as /api/v1 and include middleware for specific parts of your app which makes it easier to maintain multiple versions of an API on one server. A simple echo routes is as easy as:

var express = require('express')
var bodyParser = require('body-parser')

var app = express()
router = express.Router()
// Use middleware to parse JSON request body
router.use(bodyParser.json())

// prefix router with /api/v1/router
app.use('/api/v1', router)

// /api/v1/echo route
router.post('/echo', function(req, res) {
    // read the property message from the request body
    var message = req.body.message

    // check if message from request body is not null
    // if it it not, respond with a status of 200 and
    // send message back to client.
    // if message was null, respond with a status of
    // 400 (bad request) with an object containing
    // an error property indicating the request body is
    // missing the message property
    if(message)
        res.status(200).send({
            message: message
        })
    else
        res.status(400).send({
            errors: 'No message property in request body.'
        })
})

app.listen(3000, function(err) {
    if(err)
        return console.error(err)

    console.log('Server listening at http://localhost:3000/')
})

Comments

1

I've heard that sails.js has a very powerful cli that can get your routes up and running very quickly.

I personally like Express because the majority of people use it and if you learn express then you will be able to read a lot of other developers API code fairly easily. Once you learn express you may then want to experiment with other frameworks, but until then I would recommend just using Express.

I've heard there are also frameworks that focus on security. If you have an affection for security practices you might want to look into that one.

Socket.io is pretty awesome and allows you to see updates on a server in real time. And Hapi.js seems to be the most popular one out there. I've only messed with express, and toyed a little with Socket.io, but I would definitely learn both Express and Socket.io

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.