How to Write a Rest API

How to write a REST API?

Just to let you know:

I ended up using Ruby on Rails.

EDIT: Since this answer has been downvoted for not providing the reason behind choosing Ruby on Rails and also no instructions on how to write a REST API with it, I thought I would give you my motivation and some simple instructions.

I started reading a book about Ruby on Rails and realized that all I needed to do was to use scaffolding and I got a JSON REST API for free.

Here's a good guide to get you started: http://guides.rubyonrails.org/getting_started.html

When you have your Ruby on Rails environment up and running, creating your REST API isn't harder than running:

$ rails generate scaffold Post name:string title:string content:text

(Example from the above link.) I also found that Rails is very easy and free to deploy to heroku, which meant that I didn't have to pay for hosting for my very basic, low traffic, REST API. There are many other reasons why I am very happy to work with Ruby on Rails, but that's beyond the context of this question.

How to add a REST API to the Modern Web Dev Server?

You could write a plugin for it but I wouldn't recommend it.

It's a development server designed for testing client-side code.

It isn't designed to be a production server, and if you are going to write a REST API then you will, presumably, want it to be usable in a production environment.

Build your API with a tool designed for it (such as Express.js or Spring Boot) and use CORS to make it accessible to the server you use for client-side development.



Related Topics



Leave a reply



Submit