如何制作邮寄请求 angular 在节点的服务器上

当我在节点服务器上打印查询的内容时,我不会在任何地方看到用户数据。

这是我的节点服务器:


var http = require/'http'/;
http.createServer/ function /request, response/ {
console.log/request/;
}/.listen/8080/;
console.log/'Server running at [url=http://127.0.0.1:8080]http://127.0.0.1:8080[/url]/'/;


但是代码 Angular2:


import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Http, Response, Headers , RequestOptions } from "@angular/http";
import 'rxjs/add/operator/retry'; // to be able to retry when error occurs
import { Observable } from "rxjs/Rx";

@Component/{
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
}/

export class AppComponent implements OnInit{
title = 'Angular Test';
user = { id : 1, name : "Hello"};
constructor /private http: Http/ {}

ngOnInit//: void {
let headers = new Headers/{ 'Content-Type': 'application/json' }/;
let options = new RequestOptions/{ headers: headers }/;

console.log/this.user/;

this.http.post/"[url=http://localhost:8080/"]http://localhost:8080/"[/url], this.user, options/
.subscribe/
/err/ => {
if/err/ console.log/err/;
console.log/"Success"/;
}/;
}
}


任何人都可以帮助我或解释如何处理请求 http 在 angular.
已邀请:

冰洋

赞同来自:

这是您的服务器:


const express = require/'express'/
const bodyParser = require/'body-parser'/;
const app = express//

app.use/bodyParser.json///;
app.use/bodyParser.urlencoded/{extended: true}/ /;

app.all/"/*", function/req, res, next/{
res.header/'Access-Control-Allow-Origin', '*'/;
res.header/'Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'/;
res.header/'Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With'/;
next//;
}/;

app.post/'/ping', function /req, res/ {
res.send/req.body/
}/

app.listen/3000, function // {
console.log/'Example app listening on port 3000!'/
}/


这是您的客户 angular:


import { Component } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Component/{
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
}/
export class AppComponent {
user = { id : 1, name : 'Hello'};

constructor/private http: HttpClient/ { }

callServer// {
const headers = new HttpHeaders//
.set/'Authorization', 'my-auth-token'/
.set/'Content-Type', 'application/json'/;

this.http.post/'http://127.0.0.1:3000/ping', JSON.stringify/this.user/, {
headers: headers
}/
.subscribe/data => {
console.log/data/;
}/;
}
}


逆流
https://github.com/kuncevic/an ... mples

君笑尘

赞同来自:

我在文档页面上写了它,但现在她已经过时了,我会在这里复制它。

你的节点部分 app.js, 它应该是这样的 /假设你用了什么 expressjs 和...一起 node.js/:

app.js

:


var express = require/'express'/;
var app = express//;
var server = require/'http'/.Server/app/;
var bodyParser = require/'body-parser'/;

server.listen/process.env.PORT || 8080, function//{
console.log/"Server connected. Listening on port: " + /process.env.PORT || 8080//;
}/;

app.use/bodyParser.json///;
app.use/bodyParser.urlencoded/{extended: true}/ /;

app.use/ express.static/__dirname + '/front' / /;

app.post/'/test', function/req,res/{ //**** http request receiver ****
var myTestVar = "Hello World";
return res.send/myTestVar/;
}/;

//send the index.html on every page refresh and let angular handle the routing
app.get/'/*', function/req, res, next/ {
console.log/"Reloading"/;
res.sendFile/'index.html', { root: __dirname }/;
}/;


使用此配置节点,当您发送内容时
localhost:8080/test

, 你会得到
myTestVar

作为逆向订阅呼叫中的答案。

要回复问题请先登录注册