【问题标题】:API using mean stack application使用平均堆栈应用程序的 API
【发布时间】:2016-07-05 04:53:11
【问题描述】:

我已经安装了新的平均应用程序,我想使用该应用程序添加一个 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);

【问题讨论】:

  • 请提供require('./web/api')的代码
  • 添加了 api.js 和 models/product.js

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2017-09-05
    • 2018-02-15
    • 2017-11-28
    • 2016-11-25
    • 2017-04-27
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多