0

I have a URL which looks like http://www.example.com/idf34he8sf/9iad2hf7usnf. I want to get the params idf34he8sf and 9iad2hf7usnf

I have used below code

In angular

this.route.paramMap.subscribe(params => {
      this.organizationId = params.get("organizationId");
      this.embedId= params.get("embedId"); 
}

In Node

req.params

and

req.originalUrl

I want to get the params idf34he8sf and 9iad2hf7usnf

Ram
  • 175
  • 1
  • 2
  • 16

5 Answers5

0

Node knows nothing about Angular, you will need to design your API to take those paramaters and pass them when calling the API.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • I'm using the same URL to get params in Angular and node independently. I'm able to get queryParams but not individual params. – Ram Jul 08 '19 at 06:40
0

You can use @angular/router

import { Router } from '@angular/router';

Define a variable to hold URL

 href:string;
params:string[];

Then in Constructor

 constructor(private router: Router) {

}

In ngOnInit

this.href = this.router.url;
    this.params = this.href.split('/');
CaffeinatedCod3r
  • 821
  • 7
  • 14
0

In angular you can use ActivatedRoute to fetch params from the URL

import { Router, ActivatedRoute} from '@angular/router';

constructor(route: ActivatedRoute){}

/* Do Something*/

public someFunction(){
    this.route.queryParams.subscribe(params => {
             /* use Params*/   
    }
}

In NodeJS you will need to add while declaring your routes as shown below:

router.get('/:id', (req,res) =>{
   let id = req.params; // for parameterised routes
   // fetching query string  use below:
   // region = req.query.region;
   return res.json(id); 
});
Shubham Sharma
  • 613
  • 6
  • 14
0

You have to define params in your api as-

example/:organizationId/:embedId

then fetch these params using-

constructor(private router: Router) {
  this.organizationId = this.router.snapshots.params['organizationId']
  this.embedId = this.router.snapshots.params['embedId']
}
0
    //import the ActivatedRoute to use route.snapshot.params
    

    import { ActivatedRoute} from '@angular/router';


     //define  a variable to store the param from url 
     
     public param:any;         

    //And in constructor create a instance of ActivatedRoute
    

    constructor(private route:ActivatedRoute,) {}

    //Then in ngOnInit
    ngOnInit(): void {
    
      this.param = this.route.snapshot.params["Your-params-name in url)"];
       // params-name means (/:id/)
      //this will store the params from your url to the variable param (public)
    }
    
    
     
    //in my url is I wanted to store specific id of a book
    // from url localhost:4200/books/60ab539f678gfraep/  
    // I used  
    {path: 'books/:id',component: showBookComponent},
    //in my ROUTING MODULE  
    //so I gave above code as 
    public Id:string;
     ngOnInit():void {
    // this.Id = route.snapshot.params['id'];

    }



    
Mohammed
  • 838
  • 6
  • 14