I use Express Js with cloud functions in the back-end and Angular in the Front-end. I tried several solutions posted on Stackoverflow but no change. Here is my back-end code
const app = express();
const main = express();
main.use('/v1', app);
main.use(bodyParser.json());
main.use(bodyParser.urlencoded({ extended: false }));
// ROUTES
app.get('/posts/:postId', function(req, res, next){
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, PUT, POST, OPTIONS');
res.set('Access-Control-Allow-Headers', '*');
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
return next();
} else {
return next();
} }, function(req, res){
firebaseHelper.firestore.getDocument(db, 'posts', req.params.postId).then(
(doc) => res.status(200).send(doc),
(err) => res.status(400).send('error about getting post')); });
Angular Code
onGetPost(postId) {
const header = new HttpHeaders( { 'application/json' });
const option = {headers: header};
this.subscription = this.http.get(`https://my-url.web.app/v1/post/${postId}`,
option).subscribe(
(data: any[]) => { this.post.next(data); resolve('ok'); },
(err) => reject(err)
);
}
thank you in advance for your help