【发布时间】:2019-05-23 04:22:04
【问题描述】:
GET 方法工作正常,但是当我在我的 Angular 项目上调用 POST 方法时,似乎 nodeJs 上的 POST 函数没有被调用。我做错了什么?
角度服务:
getAllProducts(): Observable<Product[]> {
return this.http.get<Product[]>('http://localhost:3000/api/content');
}
insertProduct(product: Product): Observable<Product> {
return this.http.post<Product>('http://localhost:3000/api/content', product);
}
server.js:
app.get('/api/content', function (req, res, next) {
// query to the database and get the records
mc.query('select * from testContent', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
app.post("/api/content",function(req , res, next){
console.log('I will not get printed');
mc.query('INSERT INTO testContent (productName,productCode) VALUES (\''+req.body.productName + '\',\'' + req.body.productCode + '\')',function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
【问题讨论】:
-
参考这篇文章:How do I consume the JSON POST data in an Express application 以获得一些指导。
标签: mysql node.js angular express