I am trying to create a simple API to create different objects called entities and store them in a mongo database. I've been following the tutorial here and updating it for my own use. I'm using mongoose as well to communicate with the database, but I'm running into an issues where whenever I try and do a POST request, it returns an error due to the fact that req.body.content is undefined. Here is my server.js
file:
var express = require('express');
var bodyParser = require('body-parser');
// create express app
var app = express();
// parse requests of content-type - application/x-www-form-urlencoded
app.use( bodyParser.urlencoded( { extended: true } ) );
// parse requests of content-type - application/json
app.use( bodyParser.json() );
// database requirements
var dbConfig = require( './config/database.config.js' );
var mongoose = require( 'mongoose' );
// Configuring database
mongoose.Promise = global.Promise;
mongoose.connect( dbConfig.url );
mongoose.connection.on( 'error', function()
{
console.log( "Could not connect to the database. Exiting now..." );
process.exit();
}
);
mongoose.connection.once( 'open', function()
{
console.log( "Successfully connected to the database" );
}
);
// define a simple route
app.get( '/', function( req, res )
{
res.json( { "message" : "Welcome to API. More to come soon...." } );
}
);
// Require Entities routes
require( './app/routes/entity.routes.js' )( app );
// listen for requests
app.listen( 3000, function()
{
console.log( "Server is listening on port 3000" );
}
);
This is my external routes.js
file:
module.exports = function( app ) {
var entities = require( '../controllers/entity.controller.js' );
// Create a new Entity
app.post( '/entities', entities.create );
// Retrieve all Entities
app.get( '/entities', entities.findAll );
// Retrieve a single Entity with entityId
app.get( '/entities/:entityId', entities.findOne );
// Update an Entity with entityId
app.put( '/entities/:entityId', entities.update );
// Delete an Entity with entityId
app.delete( '/entities/:entityId', entities.delete );
}
My controller.js
file as well:
var Entity = require( '../models/entity.model.js' );
exports.create = function( req, res )
{
// create and save a new entity
console.log( 'req.body: ' + req.body );
if( !req.body.content )
{
console.log( 'req.body.content: ' + req.body.content );
return res.status( 400 ).send( { message: "Entity can not be empty" } );
}
var entity = new Entity
({
filename : req.body.filename,
material : req.body.material,
design_element : req.body.design_element,
style : req.body.style
});
entity.save( function( err, data )
{
if( err )
{
console.log( err );
res.status( 500 ).send( { message: "Some error occurred while creating the Entity." } );
}
else
{
res.send( data );
}
});
};
And an image of my post request as well. I am using Postman to test my request.
I found some stackoverflow posts already like the one here that says to set any app.use()
functions before defining any routes, and made sure that body-parser was installed, but I still get the error. Any help would be greatly appreciated.
Thanks!