我有一个现有的博客,我想采取行动。我的生产博客使用node,express和mongodb。我跟着这个guide开始运行并做出反应。我想我可以使用我设置的页面从主页上获取来自mongo的数据https://domain.com/api/all-documents
。如何从指南的app.get('*',(req, res)
文件中的app-server.js
部分向反应路由器添加API请求?
我想添加类似的内容:
const db = require('monk')('localhost/myDB')
const documents= db.get('documents')
db.close()
app.get('/api/all-documents',function(req,res){
documents.find({}, {}, function(err, docs) {
res.json(docs);
});
});
然后在我的反应页面中,我可以使用JSON数据向页面添加内容......对吗?
谢谢!