{"id":13990,"date":"2025-05-09T16:52:45","date_gmt":"2025-05-09T16:52:45","guid":{"rendered":"https:\/\/kmfinfotech.com\/blogs\/building-a-restful-web-service-with-angularjs-a-complete-tutorial\/"},"modified":"2025-05-09T16:52:45","modified_gmt":"2025-05-09T16:52:45","slug":"building-a-restful-web-service-with-angularjs-a-complete-tutorial","status":"publish","type":"post","link":"https:\/\/kmfinfotech.com\/blogs\/building-a-restful-web-service-with-angularjs-a-complete-tutorial\/","title":{"rendered":"Building a RESTful Web Service with AngularJS: A Complete Tutorial"},"content":{"rendered":"<p><br \/>\n<\/p>\n<p>\n    In today&#8217;s web development landscape, creating a robust, scalable, and efficient web application is essential. RESTful web services are at the core of many modern web applications, allowing different systems to communicate seamlessly. This tutorial aims to guide you through building a RESTful web service using AngularJS, a popular JavaScript framework for building dynamic web applications. In this comprehensive guide, we&#8217;ll cover the fundamentals of REST, AngularJS, and how to integrate the two.\n<\/p>\n<p><\/p>\n<h2>What is REST?<\/h2>\n<p><\/p>\n<p>\n    REST (Representational State Transfer) is an architectural style that defines a set of constraints and properties based on HTTP. It is primarily used for creating web services that are stateless and can be interacted with via standard HTTP methods such as GET, POST, PUT, and DELETE. REST allows clients to communicate with a server by sending requests and receiving responses in various formats, typically JSON or XML.\n<\/p>\n<p><\/p>\n<h2>Why Use AngularJS?<\/h2>\n<p><\/p>\n<p>\n    AngularJS is a powerful front-end framework maintained by Google, designed to simplify the development of single-page applications (SPAs). Its key features include two-way data binding, dependency injection, and a modular approach to application structure. AngularJS is particularly well-suited for building applications that require dynamic content updating, making it an excellent choice for integrating with RESTful services.\n<\/p>\n<p><\/p>\n<h2>Setting Up the Development Environment<\/h2>\n<p><\/p>\n<p>\n    Before we dive into coding, let\u2019s set up our development environment:\n<\/p>\n<p><\/p>\n<pre><br \/>\n1. Ensure you have the following installed:<br \/>\n   - Node.js<br \/>\n   - AngularJS<br \/>\n   - Express (for setting up the server)<br \/>\n2. Create a project directory:<br \/>\n   mkdir angular-restful-service<br \/>\n   cd angular-restful-service<br \/>\n3. Initialize a new Node.js project:<br \/>\n   npm init -y<br \/>\n4. Install Express:<br \/>\n   npm install express<br \/>\n5. Set up AngularJS by including it in our HTML file or installing it via npm:<br \/>\n   npm install angular<br \/>\n<\/pre>\n<p><\/p>\n<h2>Creating the Backend with Express<\/h2>\n<p><\/p>\n<p>\n    We will start by creating a basic RESTful API using Express. This API will manage a simple resource: a collection of books.\n<\/p>\n<p><\/p>\n<pre><br \/>\n1. Create a new file named server.js:<br \/>\n   touch server.js<br \/>\n2. Set up your Express server:<br \/>\n<\/pre>\n<p><\/p>\n<pre><br \/>\nconst express = require('express');<br \/>\nconst bodyParser = require('body-parser');<br \/>\nconst app = express();<br \/>\nconst PORT = process.env.PORT || 3000;<br>\/\/ Middleware<br \/>\napp.use(bodyParser.json());<br>let books = [<br \/>\n    { id: 1, title: '1984', author: 'George Orwell' },<br \/>\n    { id: 2, title: 'To Kill a Mockingbird', author: 'Harper Lee' },<br \/>\n];<br>\/\/ Get all books<br \/>\napp.get('\/api\/books', (req, res) => {<br \/>\n    res.json(books);<br \/>\n});<br>\/\/ Get a book by ID<br \/>\napp.get('\/api\/books\/:id', (req, res) => {<br \/>\n    const book = books.find(b => b.id == req.params.id);<br \/>\n    if (book) {<br \/>\n        res.json(book);<br \/>\n    } else {<br \/>\n        res.status(404).send('Book not found');<br \/>\n    }<br \/>\n});<br>\/\/ Add a new book<br \/>\napp.post('\/api\/books', (req, res) => {<br \/>\n    const newBook = {<br \/>\n        id: books.length + 1,<br \/>\n        title: req.body.title,<br \/>\n        author: req.body.author,<br \/>\n    };<br \/>\n    books.push(newBook);<br \/>\n    res.status(201).json(newBook);<br \/>\n});<br>\/\/ Update a book<br \/>\napp.put('\/api\/books\/:id', (req, res) => {<br \/>\n    const book = books.find(b => b.id == req.params.id);<br \/>\n    if (book) {<br \/>\n        book.title = req.body.title;<br \/>\n        book.author = req.body.author;<br \/>\n        res.json(book);<br \/>\n    } else {<br \/>\n        res.status(404).send('Book not found');<br \/>\n    }<br \/>\n});<br>\/\/ Delete a book<br \/>\napp.delete('\/api\/books\/:id', (req, res) => {<br \/>\n    books = books.filter(b => b.id != req.params.id);<br \/>\n    res.status(204).send();<br \/>\n});<br>\/\/ Start the server<br \/>\napp.listen(PORT, () => {<br \/>\n    console.log(`Server is running on http:\/\/localhost:${PORT}`);<br \/>\n});<br \/>\n<\/pre>\n<p><\/p>\n<p>\n    This basic Express API provides endpoints to create, read, update, and delete books. You can start the server with:\n<\/p>\n<p><\/p>\n<pre><br \/>\nnode server.js<br \/>\n<\/pre>\n<p><\/p>\n<h2>Building the Frontend with AngularJS<\/h2>\n<p><\/p>\n<p>\n    Now that we have the backend set up, we&#8217;ll create the AngularJS frontend. Here\u2019s how to structure our HTML file:\n<\/p>\n<p><\/p>\n<pre>\n<!DOCTYPE html><br \/>\n<html lang=\"en\" ng-app=\"bookApp\"><br \/>\n<head><br \/>\n    <meta charset=\"UTF-8\"><br \/>\n    <title>Book Management<\/title><br \/>\n    <script src=\"https:\/\/ajax.googleapis.com\/ajax\/libs\/angularjs\/1.8.2\/angular.min.js\"><\/script><br \/>\n    <script src=\"app.js\"><\/script><br \/>\n<\/head><br \/>\n<body ng-controller=\"BookController as ctrl\"><\/p>\n<div><\/p>\n<h2>Add a Book<\/h2>\n<p>\n        <input type=\"text\" ng-model=\"ctrl.newBook.title\" placeholder=\"Title\"><br \/>\n        <input type=\"text\" ng-model=\"ctrl.newBook.author\" placeholder=\"Author\"><br \/>\n        <button ng-click=\"ctrl.addBook()\">Add<\/button>\n    <\/div>\n<p><\/p>\n<h2>Book List<\/h2>\n<p><\/p>\n<ul><\/p>\n<li ng-repeat=\"book in ctrl.books\">\n            {{ book.title }} by {{ book.author }}<br \/>\n            <button ng-click=\"ctrl.deleteBook(book.id)\">Delete<\/button>\n        <\/li>\n<p>\n    <\/ul>\n<p><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>In today&#8217;s web development landscape, creating a robust, scalable, and efficient web application is essential. RESTful web services are at the core of many modern web applications, allowing different systems to communicate seamlessly. This tutorial aims to guide you through building a RESTful web service using AngularJS, a popular JavaScript framework for building dynamic web [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":13991,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[58],"tags":[268,85,363,554,238,405,74],"class_list":["post-13990","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-angularjs","tag-building","tag-complete","tag-restful","tag-service","tag-tutorial","tag-web"],"_links":{"self":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/13990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/comments?post=13990"}],"version-history":[{"count":0,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/posts\/13990\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media\/13991"}],"wp:attachment":[{"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/media?parent=13990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/categories?post=13990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kmfinfotech.com\/blogs\/wp-json\/wp\/v2\/tags?post=13990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}