Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Printing Messages to the Console

How to get some messages printed to the NodeJS Server console from my Angular App?

I have a Typescript that does some HTTP call and I want to log the HTTP end point to the server's console and I do not see anything getting printed at all, even though the API call seem to work. Here is the code snippet in my Typescript file:

    private fetchAllPowerPlants = "http://localhost:9000/powerPlants?onlyActive=false&page=1";
  loadData() {
    let url = this.fetchAllPowerPlants;
    console.log('******************************************');
    console.log(url);
    console.log('******************************************');
    return this.http.get(url)
      .map(this.extractData)
      .catch(this.handleError);
  }

This is all what I see in the console after I call the frontend!

chunk    {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 178 kB {4} [initial]
chunk    {1} main.bundle.js, main.bundle.js.map (main) 13.8 kB {3} [initial] [rendered]
chunk    {2} styles.bundle.js, styles.bundle.js.map (styles) 54.7 kB {4} [initial]
chunk    {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 2.49 MB [initial] [rendered]
chunk    {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry]
webpack: Compiled successfully.

Any ideas?

like image 740
joesan Avatar asked Sep 11 '25 15:09

joesan


1 Answers

I assume that powerPlants is a REST endpoint configured in your Node.JS server app. A simple way to log your messages on the Node server would be creating another endpoint, e.g. "/log":

let express = require("express");

const app = express(); ... app.get('/log/:msg', (req, res) => { console.log(req.params.msg); });
From the Angular side you need to make an HTTP request to this endpoint passing the message to log. Since your Angular app is not deployed on the Node server (ng serve uses dev server running on port 4200), you'll also need to configure a proxy on the client so Angular requests made to the port 4200 would be redirected to the Node server that runs on port 9000. If you're not familiar with configuting proxies on the client, watch 3 min of my presentation starting from here: https://youtu.be/k8r76d8QzXs?t=2587

like image 193
Yakov Fain Avatar answered Sep 13 '25 05:09

Yakov Fain