[英]Nodejs, Express + Angular POST 404
I am working on my MEAN stack (mySQL not MongoDB and I am also using the EJS templating engine) application and I am running into a problem with posting data into my MySQL DB. 我正在研究我的MEAN堆栈(mySQL而不是MongoDB,我也在使用EJS模板引擎)应用程序,我遇到了将数据发布到MySQL数据库的问题。 As the title says I am getting a '404' error in my console which I think indicates that the post method cannot be found.
正如标题所示,我在控制台中收到“404”错误,我认为这表明无法找到post方法。 If I putt the method in the server file, the post does work but I get a message saying 'invalid json'.
如果我把方法推到服务器文件中,帖子确实有用,但我收到一条消息“无效的json”。
Anyone has any ideas as to why this is happening? 任何人都有任何关于为什么会发生这种情况的想法?
My current code is listed as below. 我目前的代码如下所示。 I am a beginner in any of this so I would really appreciate well explained answers as that is the best way to learn where it went wrong.
我是这方面的初学者所以我非常感谢很好地解释了答案,因为这是了解它出错的最佳方式。
Server/app: 服务器/应用程序:
> 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 http = require('http');
>
> var mysql = require('mysql');
>
> var app = express();
>
> var port = 1337;
>
> // view engine setup app.set('views', path.join(__dirname, 'views'));
> app.set('view engine', 'ejs');
>
> // uncomment after placing your favicon in /public
> //app.use(favicon(__dirname + '/public/favicon.ico'));
> app.use(logger('dev')); app.use(bodyParser.json());
> app.use(bodyParser.urlencoded({ extended: false }));
> app.use(cookieParser());
> app.use(require('stylus').middleware(path.join(__dirname, 'public')));
> app.use(express.static(path.join(__dirname, 'public')));
> app.use('/bower_components', express.static(__dirname +
> '/bower_components'));
>
> var clientRoute = require('./routes/clientConfig.js');
>
> new clientRoute(app);
>
> app.use(require('./server/Dao/clientDao.js'));
>
> module.exports = app;
>
> app.set('port', process.env.PORT || 1337);
> http.createServer(app).listen(app.get('port'), function () {
> console.log('Express server listening on port ' + app.get('port')); });
Angular controller: 角度控制器:
> var clientModule = angular.module('clientModule', []);
>
> clientModule.controller('clientController', ['$scope', '$http',
> function ($scope, $http) {
> console.log("Hello world from controller");
>
> $scope.addClient = function () {
> console.log($scope.client);
>
> $http.post('/createClient', $scope.client).success(function(response) {
> console.log(response);
> })
> } }]);
Server-side post (I have no idea but it seems that it does not reference well to my server folder): 服务器端的帖子(我不知道,但似乎它不能很好地引用我的服务器文件夹):
> var express = require('express'); var router = express.Router(); var
> connectionProvider = require('../mysqlConnectionStringProvider.js');
>
> router.post('/createClient', function (req, res) {
> console.log(req.body);
>
> var connection = connectionProvider.mysqlConnectionStringProvider.getMySqlConnection();
>
> var insertStatement = "INSERT INTO Clients SET?"
>
> var client = {
> clientName : req.body.client.clientName, // this should be req.body.client.clientName
> clientAddress : req.body.client.clientAddress // this should be req.body.client.clientAddress
> }
>
>
> if (connection) {
> connection.query(insertStatement, client, function (err, result) {
>
> if (err) {
> console.log(err);
> }
>
> //console.log(result);
> });
> }
> connectionProvider.mysqlConnectionStringProvider.closeMySqlConnection(connection);
> });
>
> router.get('/createClient', function (req, res) {
>
> res.render('clients/createClient.ejs', {title : 'Add client'}); })
>
> module.exports = router;
Html: HTML:
<% include ../layout %> <div class="container" ng-app="clientModule" ng-controller="clientController"> <form class="navbar-form navbar-left" action="/createClient" method="post" name="formClient"> <div class="row"> <div class="form-group"> <label for="">Client name</label> <input type="text" class="form-control" placeholder="Please enter client name" name="clientName" ng-model="client.clientName" style="width: 100%" required> </div> </div> <div> </div> <div class="row"> <div class="form-group"> <label for="">Client address</label> <input type="text" class="form-control" placeholder="Please enter client address" name="clientName" ng-model="client.clientAddress" style="width: 100%" required> <p>{{client.clientAddress}}</p> </div> </div> <div> </div> <div class="row"> <div class="form-group"> <button class="btn btn-primary" ng-click="addClient()">Create client</button> </div> </div> </form> </div> <script src="./controllers/clients/clientController.js"></script>
Thanks in advance! 提前致谢!
To use routes from another file you can do following 要使用其他文件中的路由,您可以执行以下操作
routes/clientConfig.js 路线/ clientConfig.js
var express = require('express');
var router = express.Router();
router.get(...);
router.post(...);
router.put(...);
module.exports = router;
server.js server.js
...
app.use(require('./routes/clientConfig.js'));
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.