【发布时间】:2020-02-06 13:06:10
【问题描述】:
大家好,我的应用程序中的角度调用确实存在很大问题。首先,我将在这里传递我的代码。
角度服务
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');
}
server.js 的一部分
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);
});
});
我真的不知道这段代码有什么问题..请帮助我:(
【问题讨论】:
-
请提供更多信息。服务器启动了吗?如何在同一个端口上运行 Angular 和服务器?或者如何连接到具有不同端口的服务器而不会出现 CORS 错误?我假设您在 Node.Js 上使用 Express.Js,而 Angular CLI 用于前端,对吗?
标签: node.js angular mongodb server angular-httpclient