我正在尝试从角度服务发布数据来表达路由我没有得到数据到后端我从组件接收对象现在将它传递给节点但是没有到达那里我已经将bodyParser添加到server.js,任何想法什么是角侧失踪?
search.component.ts
this.searchService.getSearch(this.searchObj1).subscribe(searchObj => {
// this.detailService.changeMessage(searchObj.data)
consle.log(searchObj);
});
search.service.ts
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from '@angular/common/http';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class SearchService {
constructor(private http: Http) { }
getSearch(searchObj:{}) {
let body = JSON.stringify(searchObj);
console.log('Fac',body);
return this.http.post('/api/search',body)
.map(res => res.json());
}
}
路由/ api.js
const express = require('express');
const router = express.Router();
const request = require('request');
// declare axios for making http requests
const axios = require('axios');
const API = 'https://jsonplaceholder.typicode.com';
router.post('/search', (req, response) => {
// Get posts from the mock api
console.log('CLientReq',req.body);
request.post('http://sla/service/getHistoricEvents',
{ json: true,
body: req.body,
proxy:'http://one.proxy.att.com:8080',
headers: {
"content-type": "application/json",
"authorization": "Basic bTA5NDg1QGNvbXMuYXR0LmNvbTpjYnVzTVNAMjAxNw=="
}
},
function(err, res, body) {
response.status(200).json(body);
}
);
});
server.js
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
// Get our API routes
const api = require('./server/routes/api');
const Oploggery = require('Oploggery');
const app = express();
// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('/api', api);
// Catch all other routes and return the index file
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
const port = process.env.PORT || '3000' || '4200';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
server.listen(port, () => console.log(`API running on localhost:${port}`));
答案 0 :(得分:1)
尽管@angular/http
和Http
类没有任何问题,但它们现在已在Angular 5中弃用,并且自4.3以来已有替换版,请尝试HttpClient
类:
constructor(private httpClient: HttpClient) { }
getSearch(searchObj:{}) {
console.log('Fac', searchObj);
return this.httpClient.post('/api/search', searchObj);
}
我怀疑您的问题可能与标题或编码有关,HttpClient
通常没有像Http
那样多的问题。它也不要求将响应映射到res.json()
。
您可能已经在执行此操作而您没有发布它,但您需要从router
文件中导出api.js
对象:
module.exports = router;
另一个问题是服务器没有将CORS标头发送回客户端,解决此问题的最简单方法是使用cors
库:https://github.com/expressjs/cors
最简单的是,您可以对server.js
文件执行以下操作:
// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const cors = require('cors'); // Add this after installing the cors library.
// Get our API routes
const api = require('./server/routes/api');
const Oploggery = require('Oploggery');
const app = express();
app.use(cors()); // Add this.
但我强烈建议您阅读CORS和cors
库,如果您打算使用它。