检索服务器端的客户端(html / controller)数据(express / node.js)

时间:2017-05-03 05:53:10

标签: angularjs node.js mongodb mongojs

我是使用MEAN堆栈构建应用程序的新手,我正在尝试构建一个查询存储在mongodb集合中的程序数据的网站。在我的html索引文件中,我实现了一个搜索栏:

  <form class="navbar-form navbar-right" role="search">
    <div class="input-group">
    <input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term" ng-model="keywords">
    <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search" ng-click="mongoQuery()"></i></button>
    </div>
</div>
  </form>

我的controller.js文件然后启动对服务器的调用,以获取与搜索到的关键字匹配的所有mongodb集合:

myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {

  $scope.mongoQuery = function(){
    console.log($scope.keywords);
    $http.get('/search_results', $scope.keywords);
  }
}]);

然后我尝试检索与我的server.js文件中的搜索栏中输入的关键字匹配的所有记录(通过其标记字段或通过其类别字段):

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('mydb', ['bbctest']);
var bodyParser = require('body-parser');

app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());

// THIS FUNCTION DOES NOT WORK
app.get('/search_results', function(req, res){
  //console.log(req.body);
  console.log("I recieved a GET request for searching keywords")
  var searchKeywords = req.body.keywords;
  console.log(searchKeywords);
  db.bbctest.find( { $or: [ { categories:searchKeywords} , {tags:searchKeywords } ] },  { complete_title: 1 } ).forEach(function(err, docs) {
    console.log(docs);
    res.json(docs);
  });
});

app.listen(3000);
console.log("Server running on port 3000");

我试图从控制器访问变量$ scope.keywords但不知道如何。如上所示,我尝试使用 req.body.keywords 但是当我使用搜索栏时返回一个空对象。

我将不胜感激任何帮助!此外,如果有任何有用的资源,你们会建议,这将有助于我建立这样一个伟大的应用程序(搜索引擎)!

非常感谢。

1 个答案:

答案 0 :(得分:0)

使用带有get请求的查询参数发送参数和值

$scope.mongoQuery = function(){
    console.log($scope.keywords);
    $http.get('/search_results?keywords='+$scope.keywords);

   //when using post request use like below 
   // $http.post('/search_result',{keywords:$scope.keywords});
  }

添加app.use(bodyParser.urlencoded({extended:false}));在app.use之前(bodyParser.json());

var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('mydb', ['bbctest']);
var bodyParser = require('body-parser');

app.use(express.static(__dirname + "/public"));

//add below line
 app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());


//when using post request -> change app.get to app.post
app.get('/search_results', function(req, res){
  //console.log(req.body);
  console.log("I recieved a GET request for searching keywords")

  //var searchKeywords = req.body.keywords;  //when using post request 

 //accessing query parmas
  var searchKeywords = req.query.keywords; 

  console.log(searchKeywords);
  db.bbctest.find( { $or: [ { categories:searchKeywords} , {tags:searchKeywords } ] },  { complete_title: 1 } ).forEach(function(err, docs) {
    console.log(docs);
    res.json(docs);
  });
});

app.listen(3000);
console.log("Server running on port 3000");