Creating a RESTful API with Nolang

Introduction In this tutorial, we will build a simple RESTful API for managing books using Nolang. We'll define the data schema for a book, set up HTTP endpoints for CRUD operations, and create a user-friendly frontend using HTML and Bootstrap to interact with the API. Prerequisites Basic understanding of RESTful APIs Familiarity with JSON and HTML Nolang executable installed on your machine (Windows or Linux) MongoDB installed and running locally Project Setup Step 1: Set Up Your Nolang Project Create a new directory for your project: mkdir nolang-restful-api cd nolang-restful-api Create a file named app.json with the following content: { "name": "RESTful API Sample", "schemas": [ { "$id": "book", "properties": { "Title": { "type": "string", "minLength": 1 }, "Author": { "type": "string", "minLength": 5 }, "PublishedDate": { "type": "date" }, "Summary": { "type": "string", "minLength": 20, "maxLength": 200 } }, "required": ["Title", "Author", "PublishedDate"] } ], "storage": { "adapter": "mongodb", "url": "mongodb://localhost:27017", "database": "nolangtest", "id": "_id" }, "endpoints": [ { "type": "http", "port": 3000, "static": "./public", "routes": [ { "path": "/book/list", "method": "get", "return": { "$$schema": "book", "$$header": { "action": "R" } } }, { "path": "/book/:id", "method": "get", "return": { "$$schema": "book", "$$header": { "action": "R", "filter": { "$$objid": "{{env.request.params.id}}" } } } }, { "path": "/book/create", "method": "post", "bodyParser": "urlencoded", "return": { "$$schema": "book", "$$header": { "action": "C" }, "Title": "{{env.request.body.Title}}", "Author": "{{env.request.body.Author}}", "PublishedDate": "{{env.request.body.PublishedDate}}", "Summary": "{{env.request.body.Summary}}" } }, { "path": "/book/update/:id", "method": "put", "bodyParser": "urlencoded", "return": { "$$schema": "book", "$$header": { "action": "U", "filter": { "$$objid": "{{env.request.params.id}}" } }, "Title": "{{env.request.body.Title}}", "Author": "{{env.request.body.Author}}", "PublishedDate": "{{env.request.body.PublishedDate}}", "Summary": "{{env.request.body.Summary}}" } }, { "path": "/book/delete/:id", "method": "delete", "return": { "$$schema": "book", "$$header": { "action": "D", "filter": { "$$objid": "{{env.request.params.id}}" } } } } ] } ] } Create a public directory and add a file named index.html with the following content: Book Management App body { padding: 20px; } table { margin-top: 20px; } form { margin-bottom: 20px; } Book Management App Create a New Book Title: Author: Published Date: Summary: Create Book Update a Book Title: Author:

Feb 12, 2025 - 23:16
 0
Creating a RESTful API with Nolang

Introduction

In this tutorial, we will build a simple RESTful API for managing books using Nolang. We'll define the data schema for a book, set up HTTP endpoints for CRUD operations, and create a user-friendly frontend using HTML and Bootstrap to interact with the API.

Prerequisites

  • Basic understanding of RESTful APIs
  • Familiarity with JSON and HTML
  • Nolang executable installed on your machine (Windows or Linux)
  • MongoDB installed and running locally

Project Setup

Step 1: Set Up Your Nolang Project

  1. Create a new directory for your project:

    mkdir nolang-restful-api
    cd nolang-restful-api
    
  2. Create a file named app.json with the following content:

    {
        "name": "RESTful API Sample",
        "schemas": [
            {
                "$id": "book",
                "properties": {
                    "Title": { "type": "string", "minLength": 1 },
                    "Author": { "type": "string", "minLength": 5 },
                    "PublishedDate": { "type": "date" },
                    "Summary": {
                        "type": "string",
                        "minLength": 20,
                        "maxLength": 200
                    }
                },
                "required": ["Title", "Author", "PublishedDate"]
            }
        ],
        "storage": {
            "adapter": "mongodb",
            "url": "mongodb://localhost:27017",
            "database": "nolangtest",
            "id": "_id"
        },
        "endpoints": [
            {
                "type": "http",
                "port": 3000,
                "static": "./public",
                "routes": [
                    {
                        "path": "/book/list",
                        "method": "get",
                        "return": {
                            "$$schema": "book",
                            "$$header": {
                                "action": "R"
                            }
                        }
                    },
                    {
                        "path": "/book/:id",
                        "method": "get",
                        "return": {
                            "$$schema": "book",
                            "$$header": {
                                "action": "R",
                                "filter": {
                                    "$$objid": "{{env.request.params.id}}"
                                }
                            }
                        }
                    },
                    {
                        "path": "/book/create",
                        "method": "post",
                        "bodyParser": "urlencoded",
                        "return": {
                            "$$schema": "book",
                            "$$header": {
                                "action": "C"
                            },
                            "Title": "{{env.request.body.Title}}",
                            "Author": "{{env.request.body.Author}}",
                            "PublishedDate": "{{env.request.body.PublishedDate}}",
                            "Summary": "{{env.request.body.Summary}}"
                        }
                    },
                    {
                        "path": "/book/update/:id",
                        "method": "put",
                        "bodyParser": "urlencoded",
                        "return": {
                            "$$schema": "book",
                            "$$header": {
                                "action": "U",
                                "filter": {
                                    "$$objid": "{{env.request.params.id}}"
                                }
                            },
                            "Title": "{{env.request.body.Title}}",
                            "Author": "{{env.request.body.Author}}",
                            "PublishedDate": "{{env.request.body.PublishedDate}}",
                            "Summary": "{{env.request.body.Summary}}"
                        }
                    },
                    {
                        "path": "/book/delete/:id",
                        "method": "delete",
                        "return": {
                            "$$schema": "book",
                            "$$header": {
                                "action": "D",
                                "filter": {
                                    "$$objid": "{{env.request.params.id}}"
                                }
                            }
                        }
                    }
                ]
            }
        ]
    }
    
    
  3. Create a public directory and add a file named index.html with the following content:

    
     lang="en">
    
         charset="UTF-8">
         name="viewport" content="width=device-width, initial-scale=1.0">
        </span>Book Management App<span class="nt">
        
         rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
        
    
    
     class="container">
         class="text-center">Book Management App
    
        
         id="createBookForm" class="mb-4">
            

    Create a New Book

    class="form-group"> for="createTitle">Title: type="text" id="createTitle" name="Title" class="form-control" required>
class="form-group"> for="createAuthor">Author: type="text" id="createAuthor" name="Author" class="form-control" required>