API使用平均堆栈应用程序

时间:2016-06-08 08:18:09

标签: angularjs node.js mongodb express mean-stack

我安装了新的平均应用程序,我想使用该应用程序添加api。我在server.js文件中做了一些更改,我将app.use和body解析器添加到它中以获取json中的数据,并包含路由和模型以及mogodb数据库。

这是我的server.js

'use strict';


   /*
   var cl = console.log;
   console.log = function(){
  console.trace();
  cl.apply(console,arguments);
   };
  */

  process.env.NODE_CONFIG_DIR = './config/env';

    // Requires meanio .
    var mean = require('meanio');
var cluster = require('cluster');
var deferred = require('q').defer();
// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');



// Express
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

// Routes
app.use('/api', require('./web/api'));

// Code to run if we're in the master process or if we are not in debug mode/ running tests

if ((cluster.isMaster) &&
  (process.execArgv.indexOf('--debug') < 0) &&
  (process.env.NODE_ENV!=='test') && (process.env.NODE_ENV!=='development') &&
  (process.execArgv.indexOf('--singleProcess')<0)) {
//if (cluster.isMaster) {

    console.log('for real!');
    // Count the machine's CPUs
    var cpuCount = process.env.CPU_COUNT || require('os').cpus().length;

    // Create a worker for each CPU
    for (var i = 0; i < cpuCount; i += 1) {
        console.log ('forking ',i);
        cluster.fork();
    }

    // Listen for dying workers
    cluster.on('exit', function (worker) {
        // Replace the dead worker, we're not sentimental
        console.log('Worker ' + worker.id + ' died :(');
        cluster.fork();

    });

// Code to run if we're in a worker process
} else {
    var workerId = 0;
    if (!cluster.isMaster)
    {
        workerId = cluster.worker.id;
    }

  // Dependencies

// Creates and serves mean application
    mean.serve({ workerid: workerId /* more options placeholder*/ }, function (app) {
      var config = app.getConfig();
      var port = config.https && config.https.port ? config.https.port : config.http.port;
      console.log('Mean app started on port ' + port + ' (' + process.env.NODE_ENV + ') cluster.worker.id:', workerId);

      deferred.resolve(app);
    });
}

module.exports = deferred.promise;

但是当我运行localhost:3000 / api / products时,它会将我重定向到主页,即localhost:3000。

提前致谢。

api.js

// Dependencies
var express = require('express');
var router = express.Router();

// Models
var Product = require('./models/product');

// Routes
Product.methods(['get', 'put', 'post', 'delete']);
Product.register(router, '/products');

// Return router
module.exports = router;

模型/ product.js

// Dependencies
var restful = require('node-restful');
var mongoose = restful.mongoose;

// Schema
var productSchema = new mongoose.Schema({
    name: String,
    sku: String,
    price: Number
});

// Return model
module.exports = restful.model('Products', productSchema);

1 个答案:

答案 0 :(得分:0)

您的server.js文件中似乎没有连接Mongo数据库,这可能是导致问题的原因。尝试在var app = express;之后添加以下行:

mongoose.connect("mongodb://localhost/resources");

请注意,resources将是MongoDB数据库的名称。