问题描述:

I have a node Express server running on localhost that serves a page with angularjs code. When the user press a button on the page, there's an angularjs controller that post a json back to the server.

My problem is that the post doesn't seem to go to the server at all. I've checked the address and it's good. I've checked the button event and it does fire up. I'm really at a loss here. I'm new to the MEAN stack so is there something I'm doing wrong?

The route that handles the post(routes/blogposts.js):

var express = require('express');

var router = express.Router();

var MongoClient = require('mongodb').MongoClient;

var URL = 'mongodb://localhost:27017/blog';

/* POST */

router.post('/', function(req, res, next) {

process.stdout.write("hello: ");

});

module.exports = router;

The angular controller that sends the post

app.controller('PostController', ['$scope', function($scope) {

$scope.sendPost = function() {

var id = document.getElementById("postTitle").value;

var type = document.querySelector('input[name="postType"]:checked').value;

var text = document.getElementById("postText").value;

var currentDate = new Date();

var day = currentDate.getDate();

var month = currentDate.getMonth() + 1;

var year = currentDate.getFullYear();

var date = ("<b>" + day + "/" + month + "/" + year + "</b>");

var sendObject = '{"id":' + id + ', "date": ' + date + ', "type":' + type + ', "post":' +

text +'}';

$http.post('http://localhost:3000/blogPosts', sendObject);

};

}]);

EDIT: added the code to the express router below

App.js

var express = require('express');

var path = require('path');

var favicon = require('serve-favicon');

var logger = require('morgan');

var cookieParser = require('cookie-parser');

var bodyParser = require('body-parser');

var routes = require('./routes/index');

var users = require('./routes/users');

var blogposts = require('./routes/blogposts');

var app = express();

// view engine setup

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'jade');

// uncomment after placing your favicon in /public

//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));

app.use(logger('dev'));

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({ extended: false }));

app.use(cookieParser());

app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

app.use('/users', users);

app.use('/blogposts', blogposts);

// catch 404 and forward to error handler

app.use(function(req, res, next) {

var err = new Error('Not Found');

err.status = 404;

next(err);

});

// error handlers

// development error handler

// will print stacktrace

if (app.get('env') === 'development') {

app.use(function(err, req, res, next) {

res.status(err.status || 500);

res.render('error', {

message: err.message,

error: err

});

});

}

// production error handler

// no stacktraces leaked to user

app.use(function(err, req, res, next) {

res.status(err.status || 500);

res.render('error', {

message: err.message,

error: {}

});

});

module.exports = app;

网友答案:

You need to inject $http into your controller like this:

app.controller('PostController', ['$scope', '$http', function($scope, $http) {
网友答案:

The angular stuff is posting to /blogPosts and not to /.

网友答案:

following what mop said. Your angularjs controller is posting to: $http.post('http://localhost:3000/blogPosts', sendObject);

Honestly have no idea how to read your:The route that handles the post I think by showing that you make it harder to understand!. I don't think it adds any value to the question.

your code was confusing to read so i google it. http://expressjs.com/starter/hello-world.html

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

var server = app.listen(3000, function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log('Example app listening at http://%s:%s', host, port);
});

you would need something more like...

app.post('/blogPosts', function (req, res) {
      res.send('Hello World!');
    });

but even if you did this it still feels fishy to me.

相关阅读:
Top