I'm trying to create a NodeJs Proxy, the request works fine for "GET" requests, tho it fails when I try the same but with "Post"/"Delete" Requests.
I am not sure to wether is it related to frontend or to backend:(
Would you have any idea of how to fix it?
const bodyParser = require('body-parser');
const express = require('express');
const morgan = require("morgan");
const { createProxyMiddleware } = require('http-proxy-middleware');
const jsonParser = bodyParser.json()
const app = express();
const PORT = 3003;
const HOST = "localhost";
const API_SERVICE_URL = "http://127.0.0.1:3333";
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
// Proxy endpoints
app.use('/api', createProxyMiddleware({
target: API_SERVICE_URL,
changeOrigin: true,
pathRewrite: {
[`^/api`]: '',
},
}));
// Start the Proxy
app.listen(PORT, HOST, () => {
console.log(`Starting Proxy at ${HOST}:${PORT}`);
});
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ServiceService {
constructor(private httpClient: HttpClient) {
}
public createNewUser(bodyParams: any): Observable<any> {
return this.httpClient.post('http://localhost:3003/api/register', bodyParams);
}
public getTable(): Observable<any> {
return this.httpClient.get('http://localhost:3003/api/table');
}
public deleteUser(username: string): Observable<any> {
return this.httpClient.delete(`http://localhost:3003/api/delete/${username}`);
}
}