Coding / Programming Videos

Post your favorite coding videos and share them with others!

Writing a Toy REST API – Diogo Sperb – Medium

Source link

So you want to write a toy REST API, be it for prototyping a microservice or just for fun. Ok, let me show you one way you can do this with just a few lines of code.

First, we’re going to install Node.js, then a web framework called wideweb:

$ npm install -g wideweb

Then we’re going to create our little project:

$ wideweb new toy

At this point you should be able to start the service and see that everything is working alright:

$ cd toy
$ npm start

The framework will let you know that you can point your browser to http://localhost:3000/ and if you do it you’ll see something like:

Hello, world wideweb!

Good! Now you have a running app that doesn’t do much yet, but will become more interesting very soon. Open the server.js file in your favorite text editor and you should see something like:

“use strict”;

const wideweb = require(“wideweb”);

wideweb.route({
 getRoot: (req, res) => {
 res.render(“index.html”);
 }
});

This is the skeleton file provided by the framework. As you can see, it has a single endpoint for a GET method at the root ( / ) which renders the index.html view, also a skeleton file provided by the framework as it is.

We’re going to spice up our routes a little bit. Also, we’re going to create a class to represent our resources model, an array to hold its instances and some helper functions. Here is the way our server.js file will look after adding all these things:

“use strict”;

const wideweb = require(“wideweb”);

const resources = [];

class Resource {
 constructor(object) {
 const emailRegex = /^[email protected]+$/;
 this.email = [].concat(object.email).filter(selection => selection.match(emailRegex));
 this.phone = [].concat(object.phone);
 }
}

function addResource(resource) {
 resources.push(new Resource(resource));
}

function modifyResource(id, resource) {
 resources[id] = new Resource(resource);
}

function removeResource(id) {
 resources.splice(id, 1);
}

wideweb.route({
 getRoot: (req, res) => {
 res.render(“index.html”);
 },
 deleteResource: (req, res) => {
 removeResource(req.query.id);
 res.send();
 },
 getResource: (req, res) => {
 res.json(resources[req.query.id]);
 },
 getResources: (req, res) => {
 res.json(resources);
 },
 postResource: (req, res) => {
 addResource(req.body);
 res.send();
 },
 postResources: (req, res) => {
 req.body.map(addResource);
 res.send();
 },
 putResource: (req, res) => {
 modifyResource(req.query.id, req.body);
 res.send();
 }
});

Now you should stop the service (Ctrl + C) and start it again:

$ npm start

Then you can use cURL (or Postman) to play CRUD with your fresh resources a little bit. Let’s check some sample requests:

curl -X POST
 http://localhost:3000/resources
 -H ‘Content-Type: application/json’
 -d ‘[{“email”:”[email protected]”,”phone”:”555–5555″},{“email”:”[email protected]”,”phone”:”555–5555″}]’

curl -X GET
 http://localhost:3000/resources

Bonus: As you can see, besides holding some data, our Resource class does two additional tricks. It automatically converts single strings into lists (see the literal array calling concat on whatever it receives from the parsed object) and it also knows some simple email regex to filter out invalid entries.

From this point, you can use this toy as a mock API to test some other software, extend it with additional models and routes or even change the helper functions to persist your entries on some database of your choice. Have fun!

Did you like this article? Please applause it, share it, follow me and and let me know what you think in the comments section. Thanks and see you next time!

Source link

Bookmark(0)
 

Leave a Reply

Please Login to comment
  Subscribe  
Notify of
Translate »