简体   繁体   English

接受来自其他网站的 POST 请求到 Angular 应用程序

[英]Accept POST request from other website to Angular Application

I want to accept post request to my Angular application from other application.我想接受其他应用程序对我的 Angular 应用程序的发布请求。

Basically they will send customer id list as a POST payload and I have to process it in my angular application & render GUI.基本上,他们会将客户 ID 列表作为 POST 有效负载发送,我必须在我的 angular 应用程序和渲染 GUI 中处理它。

I tried following app.post method in app.js but It throws error "Cannot do POST"我尝试在 app.js 中遵循 app.post 方法,但它抛出错误“无法执行 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.这是一个有点尴尬的要求,因为 Angular 是 JavaScript 框架,它不接受 post 请求,因为 post 请求只需要在服务器端处理,而不是在客户端处理。

But Is there any work around like accept post request in Node JS and send to angular application and render UI ?但是是否有任何解决方法,例如在 Node JS 中接受 post 请求并发送到 angular 应用程序并呈现 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.我有 Node JS 应用程序,主要是通过 socket.io 获取实时数据馈送。

Yes, you need a server side server to receive an request (GET or POST), like express.js.是的,您需要一个服务器端服务器来接收请求(GET 或 POST),例如 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 和 Express 使用相同的服务器,您对 localhost:3000/api/customer 执行 POST,express 将使用 JSON 或其他内容进行响应。

Angular is Client-Side, so, the POST, come from Client Computer. Angular 是客户端,因此 POST 来自客户端计算机。 You doesn't need to worry about which customer you need to response.您无需担心需要响应哪个客户。

Well, Angular is Front-end framework.好吧,Angular 是前端框架。 You cannot accept POST request from angualar.您不能接受来自angualar 的POST 请求。 But you can achieve this by making use of any server-side service like nodejs or java.但是您可以通过使用任何服务器端服务(如 nodejs 或 java)来实现这一点。 You can use nodejs for the same.您可以使用 nodejs 来做同样的事情。 1. Create a server-side using express or any other framework in nodejs. 1. 在 nodejs 中使用 express 或任何其他框架创建服务器端。 2. Your client then have to POST their cutomer id or other data onto that server. 2. 然后您的客户必须将他们的客户 ID 或其他数据发布到该服务器上。 3. Use a database such as mongodb to store data. 3、使用mongodb等数据库来存储数据。 4. Then, atlast you need to get data from your server using GET request. 4. 然后,最后您需要使用 GET 请求从您的服务器获取数据。 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.就像你最后说的那样,你得到了一个带有 socket.io 的 nodejs 应用程序,很好,当你的客户端发布数据时,你可以通过监听 socket.io 发出的事件来获取实时数据。 Then you can show those data in your angular app.然后你可以在你的 angular 应用程序中显示这些数据。 This can be accomplished with socket.io-client in angular.这可以通过 socket.io-client 在 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.但是,请记住,每次重新启动应用程序时,您都必须向服务器发送 GET 请求以再次从数据库中获取数据(如果您正在存储它们),因为 angular 只是一个前端框架。 Hope this helps you.希望这对你有帮助。 You can comment if you got any question.如果您有任何问题,可以发表评论。 I'll be happy to help!我很乐意提供帮助!

You need to use Angular universal.您需要使用 Angular 通用。 Here is the official guide Angular universal app这是官方指南Angular 通用应用程序

Then in the file server.ts然后在文件 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.看到Angular是前端技术,做接收post请求几乎是不可能的。 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.这是因为 Angular 在默认情况下会创建一个服务器并在“ng serve”上运行它,而您可能真的无权访问该服务器。

However, on deploying your application, you might need to create your own server (eg when deploying to heroku).但是,在部署应用程序时,您可能需要创建自己的服务器(例如部署到 heroku 时)。 When you create your server.js file, you can handle all kinds of api calls from your server file.创建 server.js 文件时,您可以处理来自服务器文件的各种 api 调用。

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.然而,问题在于您的这些请求在您的本地机器上不起作用,因为您刚刚创建的服务器在 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)另一方面,这是使用邮递员的帖子请求,并且有效(我在截屏之前清除了网址)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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