Node.js 角度中的http调用有问题。我不能寄
嗨,伙计们,我在我的应用程序中遇到了一个很大的问题。首先,我将在这里通过我的代码 角度服务Node.js 角度中的http调用有问题。我不能寄,node.js,angular,mongodb,server,angular-httpclient,Node.js,Angular,Mongodb,Server,Angular Httpclient,嗨,伙计们,我在我的应用程序中遇到了一个很大的问题。首先,我将在这里通过我的代码 角度服务 path = 'http://localhost:3000'; httpOptions = { headers: new HttpHeaders({'Content-Type': 'application/json'}) }; constructor(private http: HttpClient) { } getProducts(): Observable <any>
path = 'http://localhost:3000';
httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
constructor(private http: HttpClient) { }
getProducts(): Observable <any> {
return this.http.get<any>(this.path + '/api/products');
}
错误:
我真的不知道这个代码有什么问题。。请帮助我:(此错误的原因是我使用了Angular Tour of Heroes应用程序作为我的基础,并且我没有删除依赖项 “角度内存web api”:“~0.2.4”
就是这样…这个问题可能有多种原因: 我将采用以下方法:
请提供更多信息。服务器启动了吗?如何在同一端口上运行angular和服务器?或者如何连接到具有不同端口的服务器而不出现CORS错误?我假设您在Node.Js上使用Express.Js,在前端使用angular CLI,对吗?
client.connect(err => {
collectionUsers = client.db("users").collection("users");
hivesDatabase = client.db("hives");
});
app.listen(3000, () => {
console.log('Server started at port 3000');
});
app.use(bodyParser.json());
app.route('/api/products').get(async (req, res) => {
let response = [];
collectionUsers.find().toArray().then(items => {
items.forEach(item => {
response.push(item);
});
res.send(response);
});
});