GithubHelp home page GithubHelp logo

leolanese / angular-nodejs-restapi Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 722 KB

Angular + NodeJS REST Api - Full Stack Development Workflow

HTML 14.80% TypeScript 73.32% SCSS 11.88%
angular2 expressjs nodejs rest-api standalone-components typescript

angular-nodejs-restapi's Introduction

Angular (17+) + NodeJS Users Rest Api. Full-Stack Development Workflow

Goal

Show the use of Angular services to consume local and remote end-points, provided localy by NodeJS & ExpressJS and remotly using JsonPlaceHolder` Services. We will be using Angular 17+ with stand-alone component and services.


Fetching Users from my NodeJS (and ExpressJS) Rest API

// Front-End
πŸ…°οΈ Creating an Angular app + Angular Material
 - Initialise GIT repo from GITHUB
 - Create APP:
    - ng new users-rest-api --style=scss
    - code .
    - cd /user-rest-api/npm start
πŸš‰ Adding Angular Material UI
    - ng add @angular/material
πŸš™ Adding the HttpClient
    - mkdir services
    - cd src/app/services
    - ng g s API-user
    - mkdir models
    - cd src/app/services
    - ng g i users
    - using HTTP Client
πŸ›΄ Defining service & interface
    - user Interface (based on public API Service `https://jsonplaceholder.typicode.com/users`)
πŸ‘₯ Fetching & Displaying users
// Back-end
🧨 Endpoint Customisation
    - Moving out from `jsonplaceholder` to local NodeJS Rest API
    - mkdir rest-api-nodejs
    - cd rest-api-nodejs
✨ initialising NodeJS with ExpressJS 
    - npm init -y
    - npm i express
    - npm i morgan
    - npm i cors
    - notepad server.js (add content)
    - node --loader ts-node/esm server.mjs
    - Adjust the `package.json` from "type": "module" to use imports
    - Test API end-point: http://localhost:3000/users
πŸŽ‡ Adding Image Source for Users
    - Using RoboHash for random images (https://robohash.org/${user.id}.png?set=set1&size=45x4)
    - Test App: http://localhost:4200/

Demo

Local users API :3000 (Node served)

Angular View users API :4200 (Node served)


Local mock users locally to be served

// server.mjs
import express from 'express';
import cors from 'cors';

const app = express();
const PORT = 3000;

const users = [
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  {
    "id": 3,
    "name": "Clementine Bauch",
    "username": "Samantha",
    "email": "[email protected]",
    "address": {
      "street": "Douglas Extension",
      "suite": "Suite 847",
      "city": "McKenziehaven",
      "zipcode": "59590-4157",
      "geo": {
        "lat": "-68.6102",
        "lng": "-47.0653"
      }
    },
    "phone": "1-463-123-4447",
    "website": "ramiro.info",
    "company": {
      "name": "Romaguera-Jacobson",
      "catchPhrase": "Face to face bifurcated interface",
      "bs": "e-enable strategic applications"
    }
  },
  {
    "id": 4,
    "name": "Patricia Lebsack",
    "username": "Karianne",
    "email": "[email protected]",
    "address": {
      "street": "Hoeger Mall",
      "suite": "Apt. 692",
      "city": "South Elvis",
      "zipcode": "53919-4257",
      "geo": {
        "lat": "29.4572",
        "lng": "-164.2990"
      }
    },
    "phone": "493-170-9623 x156",
    "website": "kale.biz",
    "company": {
      "name": "Robel-Corkery",
      "catchPhrase": "Multi-tiered zero tolerance productivity",
      "bs": "transition cutting-edge web services"
    }
  },
  {
    "id": 5,
    "name": "Chelsey Dietrich",
    "username": "Kamren",
    "email": "[email protected]",
    "address": {
      "street": "Skiles Walks",
      "suite": "Suite 351",
      "city": "Roscoeview",
      "zipcode": "33263",
      "geo": {
        "lat": "-31.8129",
        "lng": "62.5342"
      }
    },
    "phone": "(254)954-1289",
    "website": "demarco.info",
    "company": {
      "name": "Keebler LLC",
      "catchPhrase": "User-centric fault-tolerant solution",
      "bs": "revolutionize end-to-end systems"
    }
  }
];

app.use(cors()); // Enable CORS for all routes

// Checking if the application is in development mode
// If it is, morgan middleware is added to the express application to log HTTP requests
if(process.env.MODE === 'development'){
  app.use(morgan('dev'))  
}

app.use(express.json());

app.get('/users', (req, res) => {
  res.json(users);
});

// Starting the express server and listening for connections on the specified port.
app.listen(PORT, (err) =>  
    (err) ? console.error(`Failed to start server: ${err}`) :  console.log(`Server running on: ${PORT}`)
);

This project was generated with Angular CLI version 17.0.9.

Development server

Run ng serve for a dev server. Navigate to http://localhost:4200/. The application will automatically reload if you change any of the source files.

Code scaffolding

Run ng generate component component-name to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module.

Build

Run ng build to build the project. The build artifacts will be stored in the dist/ directory.

Running unit tests

Run ng test to execute the unit tests via Karma.


πŸ’― Thanks!

Now, don't be an stranger. Let's stay in touch!

leolanese’s GitHub image
πŸ”˜ Linkedin: LeoLanese
πŸ”˜ Twitter: @LeoLanese
πŸ”˜ Portfolio: www.leolanese.com
πŸ”˜ DEV.to: dev.to/leolanese
πŸ”˜ Blog: leolanese.com/blog
πŸ”˜ Questions / Suggestion / Recommendation: [email protected]

angular-nodejs-restapi's People

Contributors

leolanese avatar

Stargazers

krabat1 avatar  avatar

Watchers

 avatar krabat1 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.