I have installed the new mean app and i want to add an api using that app. I have done some changes in server.js file i add the app.use and body parser to it to get data in json and included the route and the model and the mogodb database.
This is my 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;
But when i run the localhost:3000/api/products it redirect me to the home page ie localhost:3000.
Thanks in advance.
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;
model/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);
It doesn't look like you are connecting your Mongo database in your server.js
file and that may be the cause of your issues. Try adding the following line after var app = express;
:
mongoose.connect("mongodb://localhost/resources");
Note that resources
would be the name of your MongoDB database.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.