0

I'm using angular 2. I have one component on front-end that calls with HttpClient get my function on back-end. My code on localhost works fine running "node server.js" and "ng serve", but when i put it on firebase (with "ng build --prod" and "firebase deploy") it gives me that error when load the page:

{
error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.l
headers: t {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for https://my-project-name.firebaseapp.com/test"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
}

I have tried to force the responseType as suggested in that answer Angular HttpClient "Http failure during parsing" but it gives me a compile error that the response is json and not text.

thats my code that works on localhost:

//file .ts
import { HttpClient } from '@angular/common/http';
...
...
constructor(private http: HttpClient){}
ngOnInit() {
    this.http.get<{}>('http://localhost:8080/test').subscribe(res => {
      console.log(res);
    });
...
...
//file server.js
//Install express server
const express = require('express');
const path = require('path');

const app = express();

// Serve only the static files form the dist directory
app.use(express.static(__dirname + 'localhost:4200'));

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  );
  res.setHeader(
    'Access-Control-Allow-Methods',
    'GET, POST, PATCH, PUT, DELETE, OPTIONS'
  );
  next();
});

app.route('/test').get((req, res) => {
  res.send({
    cats: [{
      name: 'lilly'
    }, {
      name: 'lucy'
    }],
  })
});

app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname + 'localhost:4200'));
});

app.listen(process.env.PORT || 8080, () => {
  console.log('online');
});

thats my code that doesn't work on firebase

//file .ts
import { HttpClient } from '@angular/common/http';
...
...
constructor(private http: HttpClient){}
  ngOnInit() {
    this.http
      .get<{}>('https://my-project-name.firebaseapp.com/test')
      .subscribe(res => {
        console.log(res);
      });
...
...
//server.js
//Install express server
const express = require('express');
const path = require('path');

const app = express();

// Serve only the static files form the dist directory
app.use(express.static(__dirname + '/dist/browser'));

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept, Authorization'
  );
  res.setHeader(
    'Access-Control-Allow-Methods',
    'GET, POST, PATCH, PUT, DELETE, OPTIONS'
  );
  next();
});

app.route('/test').get((req, res) => {
  res.send({
    cats: [{
      name: 'lilly'
    }, {
      name: 'lucy'
    }],
  })
})

app.get('/*', function (req, res) {
  res.sendFile(path.join(__dirname + '/dist/browser/index.html'));
});

app.listen(process.env.PORT || 8080);
rever
  • 178
  • 2
  • 13
  • Try the resposeType as `responseType: 'text' as 'json'` – Ashish Ranjan Apr 26 '19 at 07:53
  • The URL https://ecommerce-518a4.firebaseapp.com/test returns your root HTML page. It doesn't return JSON as you think it should do. Why do you think it should return JSON, sine you don't have any server anymore that generates JSON? – JB Nizet Apr 26 '19 at 08:03
  • I accidentally forgot to hide the link, can you edit your comment please? – rever Apr 26 '19 at 08:24
  • on localhost it works correctly, what i should do do makes it work on firebase? – rever Apr 26 '19 at 08:29
  • 1
    Read the documentation of Firebase to understand what it is. It's not an express hosting provider. – JB Nizet Apr 26 '19 at 08:36
  • using ```responseType: 'text' as 'json'``` I get no error, but in the browser console I get back my index.html and the back-end function code seems to not run – rever Apr 26 '19 at 08:37
  • upvote my question pls... I have too many questions without upvote and I can't question again..... thats sad – rever Nov 20 '19 at 12:04

1 Answers1

0

Apparently firebase doesn't support my code pattern (express server-side). I deployed on heroku (and I changed the link on the code), it works. thanks for your help. If someone find out how to use that pattern on firebase free plan feel free to advise me.

rever
  • 178
  • 2
  • 13