Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  142] [ 6]  / answers: 1 / hits: 25728  / 9 Years ago, mon, march 16, 2015, 12:00:00

I am trying to send POST data from AngularJS to Express. What I am doing at the moment is trying to send some POST stuff via curl.



I already tried sending the data. It gets send, I get 200 as a response. But whenever I try to access the data via body-parser through req.body I will get a 500 and the error that I posted at the very bottom.



I did a lot of research on that matter (past 4-5 hours...) and I think I checked the following common issues:




  • I use some sort of request parser: body-parser

  • I put my app.use(...) with body-parser middleware before requiring for routes

  • I explicitly tell what Content-Type the POST uses

  • I tried both application/json and application/x-www-form-urlencoded



I surrender. StackOverflowers. Help me, please.



curl:



curl -X POST -H Content-Type: application/json -d '{username: gala, email: [email protected], password: 123}' localhost:3000/users


express.js



// glowny plik przygotowujacy aplikacje express

// wczytanie zaleznosci
var express = require('express')
var stylus = require('stylus')
var parser = require('body-parser')
var morgan = require('morgan')
var compress = require('compression')
var methodOverride = require('method-override')

// przygotowanie aplikacji, obsluga middleware i na koncu zwrocenie obiektu aplikacji
module.exports = function() {
var app = express()

if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
} else if (process.env.NODE_ENV === 'production') {
app.use(compress())
}

app.use(parser.urlencoded({ extended: true }))
app.use(parser.json())

app.use(methodOverride())

// uzycie jade jako silnika szablonow dla html
app.set('views', __dirroot + '/app/views')
app.set('view engine', 'jade')

require(__dirroot + '/app/routes/index.server.route.js')(app)
require(__dirroot + '/app/routes/users.server.route.js')(app)

// mozliwosci uzywania statycznych plikow z folderu /public
app.use(express.static(__dirroot + '/public'))

return app
}


users.server.route.js:



var users = require(__dirroot + '/app/controllers/users.server.controller.js')

module.exports = function(app) {
app.route('/users').post(function() {
users.create()
})
}


users.server.controller.js:



var User = require('mongoose').model('User')

exports.create = function(req, res, next) {
console.log(req.body)
}


Error message:



TypeError: Cannot read property 'body' of undefined
at Object.exports.create (/home/gala/Projekty/cantr-crafting/app/controllers/users.server.controller.js:4:20)
at /home/gala/Projekty/cantr-crafting/app/routes/users.server.route.js:5:15
at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/layer.js:82:5)
at /home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:321:12)
at next (/home/gala/Projekty/cantr-crafting/node_modules/express/lib/router/index.js:261:10)
at methodOverride (/home/gala/Projekty/cantr-crafting/node_modules/method-override/index.js:77:5)

More From » node.js

 Answers
32

I don't use Node or Express much but shouldn't you at least pass req into users.create() from the route handler, ie



app.route('/users').post(function(req, res) {
users.create(req, res)
})


or maybe even



app.route('/users').post(users.create);

[#67431] Thursday, March 12, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aiyanakatelinp

Total Points: 578
Total Questions: 110
Total Answers: 111

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;