简体   繁体   中英

Accept POST request from other website to Angular Application

I want to accept post request to my Angular application from other application.

Basically they will send customer id list as a POST payload and I have to process it in my angular application & render GUI.

I tried following app.post method in app.js but It throws error "Cannot do POST"

app.post('/customer', function (req, res) {
    console.log(req.body);
});

It is a bit awkward requirement since Angular is JavaScript framework and It does not accept post request because post request needs to be handled at server side only not at client side.

But Is there any work around like accept post request in Node JS and send to angular application and render UI ? Might be a dumb idea so need some idea or work around at least.

Edit:= More Information

I have Node JS application which is mainly to get live data feed through socket.io.

Yes, you need a server side server to receive an request (GET or POST), like express.js. If you use the same server for Angular and Express, you do a POST to localhost:3000/api/customer, and express will response with JSON or something.

Angular is Client-Side, so, the POST, come from Client Computer. You doesn't need to worry about which customer you need to response.

Well, Angular is Front-end framework. You cannot accept POST request from angualar. But you can achieve this by making use of any server-side service like nodejs or java. You can use nodejs for the same. 1. Create a server-side using express or any other framework in nodejs. 2. Your client then have to POST their cutomer id or other data onto that server. 3. Use a database such as mongodb to store data. 4. Then, atlast you need to get data from your server using GET request. It's simple as that.

Like you said in the end, you got a nodejs app having socket.io, well that's good you can get live data by listening to events emitted by socket.io when your client post data. Then you can show those data in your angular app. This can be accomplished with socket.io-client in angular. But, remember you have to send a GET request to server to fetch data again from database(if you're storing them) everytime you restart the application as angular is only a front-end framework. Hope this helps you. You can comment if you got any question. I'll be happy to help!

You need to use Angular universal. Here is the official guide Angular universal app

Then in the file server.ts

You could accept pots requests by adding.

 server.post('/customer', (req, res) => {
         console.log(req.body);

 });

If you want to get the body you can check those answers .

Seeing that Angular is a front end technology, it is almost impossible to do receive post requests. This is because Angular, by default, creates a server and runs it on 'ng serve' and you might really not have access to this server.

However, on deploying your application, you might need to create your own server (eg when deploying to heroku). When you create your server.js file, you can handle all kinds of api calls from your server file.

The problem with this, however, is that you those requests will not work on your local machine, because the server you have just created does not work on localhost.

const PORT = process.env.PORT || 8905;
const path = require('path');
const express = require('express');
const forceSsl = require('force-ssl-heroku');
const app = express();

app.use(forceSsl);
app.use(express.static(__dirname + '/dist/fenix-school'));
app.use(express.json());

app.post('/payment/success', (request, response) => {
  response.json(request.body)
});

app.get('*', (request, response) => {
  console.log('route hit');
  response.sendFile(path.join(__dirname + '/dist/fenix
 school/index.html'));
});

app.listen(PORT, () => console.log(PORT));

Here, I have created created a server and let it handle a post request by just returning the request body

1

On the other hand, this is a post request from using postman, and it works (I cleared out the url before taking the screenshot)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM