【发布时间】:2019-11-21 11:44:44
【问题描述】:
我正在尝试使用 Nodejs 服务器作为代理服务器来绕过特定 API 的 CORS,例如 darksky.net 或 googleapis。如下面的 Angular 8 代码所示,我尝试向我的 NodeJS 服务器发送一个获取请求,并传递三个参数。 NodeJs 服务器收到这些参数后,我请求 API,但得到 404 错误作为回报。
角度代码:
this.http.get('search/coords/',
{
params: {
address: this.street,
city: this.city,
state: this.state
}
}).subscribe(data => {
this.lattitude = data['results']['geometry']['location']['lat'];
this.longitude = data['results']['geometry']['location']['lon'];
console.log(this.lattitude);
console.log(this.longitude);
this.coords = {
lat: this.lattitude,
lon: this.longitude
};
});
return this.coords;
}
这是我当前的 Nodejs/Express 代码:
const express = require('express')
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
var request = require('request');
const app = express();
var url = "";
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({'extended': 'false'}));
app.use(cors());
app.get('search/coords/', function (req, res) {
var street = req.query.address;
var city = req.query.city;
var state = req.query.state;
url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + street + "," + city + "," + state + "&key=blah/"
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
res.send(info);
}
})
});
具体来说,我收到一个 GET 404 not found 错误和一个 ERROR HttpErrorResponse {headers: HttpHeaders, status: 404, statusText: "Not Found", url: "http://localhost:4200/search/coords/?address......." 我是Angular 和 nodejs 的新手,因此非常感谢任何帮助。
【问题讨论】:
标签: node.js ajax angular get request