API with Javascript

Asked

Viewed 1,072 times

3

I would like to know how to make an API in the javascript language to return data as JSON. I need because I want to make a mobile app.

  • 1

    How are the data you want to return as JSON?

  • for example, I want to create a url that returns a set of objects, for example, "http://exemploapi.com/clients" return = {"id":"1", "status": "0", "error":"","message":"Test"}

  • 1

    You’re using JS on the server, right? Node.js?

  • Yes. I will use Node.JS on the server

1 answer

2


One way and make an API is to create a separate Javascript file containing one or more objects declaring the functions you need in your API.

For example, below is a dummy API representing a simple calculator:

// calculadora. js
var calculadora = {
    somar: function (a, b) {
        return a + b;
    },

    multiplicar: function (a, b) {
        return a * b;
    }
};

To use your API you first need to import the Javascript file from the API into the page through a script tag:

<script type="text/javascript" src="pasta_da_api/calculadora.js" />

Once done, you can now call the methods declared in your API like this:

var soma = calculadora.somar(a, b);

var produto = calculadora.multiplicar(a, b);
  • And where JSON comes in?

  • The API itself in this example I gave is a JSON object. You can return anything in the methods of your api, including a json. I will edit the answer by adding a new method that returns a JSON to serve as an example.

  • I want to return this data via url: example of url: http://interactividade.globo.com/notas/agregador/torcidometro_14417/resultado-structuraccomplicated.json

  • So in this case what you need is a Webservice, not a Javascript API. But by your question implied what I answered, that you wanted to create an API in Javascript.

  • Um, sorry if I said wrong, do you know any links that are explaining something about webservices?

  • But do it, Danilo. Create another question, why this one will also be useful to the community. In the new question you inform that you need a Webservice that returns data in JSON format. There are many options, and virtually any backend technology offers this feature. As a programmer . NET I use Servicestack. It may be a good option for you too if you program in some language . NET.

  • Ulysses, that your code is not a "JSON object", it’s Javascript. JSON are serialized objects (like strings), and the format does not accept functions. This code of yours could never be serialized (but the returns of the functions could).

  • @bfavaretto I believe you are mistaken at some points. Jsons and strings are your different things. For example, JSON {Name: "John", age: 13} can set converted to string via the JSON.stringfy method, which would return the string '{"name": "John", "age": 13}'. And to convert string to JSON has the JSON.parse method. logo, strings and Jsons are different things, ie, are different ways to represent the same information. Therefore my object is in fact a JSON.

  • Not Ulysses, you are confusing literal Javascript objects with JSON. This confusion is quite common, as they are quite similar. JSON is based on JS literals - including "JS" in "JSON" means Javascript. JSON is a data exchange format between systems or machines, which accepts a limited set of types (and functions are not accepted in JSON), with rules clear representation (like key quotes, optional in JS literals). The JS methods you quoted convert from JSON to values/objects, and from values/objects to JSON.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.