node.js: RESTful API

--

Hi people!!

Today I am going to show you how to build an API using RESTful.

1st step: Open the command line and enter the following commands:

mkdir RESTful
npm init
touch api.js
npm install express -S

This was the result of the necessary installation:

2nd step: Insert the following code

var express = require('express');var app = express();app.get('/',function(req, res) {res.send("RESTful API - Example");});var users = [{ id: 1, username: 'Afonso Antunes', email: 'afonso__antunes@outlook.com'},{ id: 2, username: 'Bruno Teixeira', email: 'btt@uci.com'},{ id: 3, username: 'Ruben Fernandes', email: 'rdf@sapo.pt'},];app.get('/listaUsers',function(req, res, next) {res.send(users);});app.listen(8080, function() {console.log("Servidor ativo no porto 8080");});

3rd step: Run the following command

node api.js

And this was the result:

http://localhost:8080/listaUsers

I hope you enjoyed!!

--

--

Afonso Antunes
Afonso Antunes

Written by Afonso Antunes

Degree in Computer Engineering, Master in Cybersecurity

No responses yet