如何使用nodejs使网页将数据传递到后端(服务器并保存到数据库)

时间:2016-03-17 09:41:30

标签: angularjs node.js

我是nodejs的新手。我的问题很简单。尝试使用nodejs表达framwork(在eclipse IDE中)来制作一个示例网站。本网站包含两页,一页是酒店预订,在此页面,客户可以选择房间类型(特大床房,大床房),选择需要多少房间,总价将自动显示在此页(使用angularjs)。还有一个按钮自定义可以点击结帐。我已经使用下面附带的angularjs,代码(hotelOder.ejs)完成了这个页面。
我的问题来自下一步:点击结帐后。我想在此页面上将3个值(房间类型,订购房间数量,订单总数)发送到后端(服务器)并使用nodejs将其保存到服务器的数据库中。无法使用nodejs(下面的transfer.js文件)来判断gout如何做到这一点。然后,如果数据库已经成功保存了这三个值,那么让服务器响应保存的" order total"回到第二页(orderConfirm.ejs,附在下面)。有人可以帮忙吗?知道如何编码上传(或称为post?)这三个值到后端并保存到数据库(使用mysql)。

transfer.js

var ejs = require("ejs"); 
function orderConfirm(req,res)
{
   var totalpay = req.param("totalPay");
   //save to database ???
   console.log("Query is:"+totalpay);
   //return order total ???
   return res.render('orderConfirm', {data: totalpay}); 
}
exports.orderConfirm=orderConfirm;

orderConfirm.ejs

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body>
<p>ordered</p>
<div>          
                <p><strong>EmailId: </strong><%= data %></p>
<br>                 
</div>

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 transfer = require('./routes/transfer'); //add label

var app = express();

// 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(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.post('/orderConfirm', transfer.orderConfirm);//label add

// 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;

指数(hotelOrder).ejs

    <head>
     <meta charset="UTF-8">
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<style>
    .show{
        display:block;
    }
    .hidden{
        display:none;
    }
</style>
</head>
<body>

<div >
    <div ng-controller="CartController"  >
         <h1></h1>

        <table>
            <tr>
                <th>Available Rooms</th>                
                <th>Quantity</th>
                <th>Price</th>
                <th>Total Price</th>
                <th>delete</th>
            </tr>
            <tr ng-repeat="item in items">
                <td>{{ item.title }}</td>
                <td>
                    <input ng-model="item.quantity" />
                </td>
                <td>{{ item.price | currency }}</td>
                <td>{{ item.price * item.quantity | currency }}</td>
                <td>
                    <button ng-click="remove(index)">delete</button>
                </td>
            </tr>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th id="totalPay2">{{ totalPirce() | currency }}</th>
                <th> <button type="" ng-click="checkout()">Order</button> </th>              

            </tr>
        </table>


        <div ng-class="{true:'show',false:'hidden'}[isShow]">
         <table>
            <tr>
                <th><h1>Receipt</h1></th>
            </tr>
            <tr>
                <th>name</th>                
                <th>john</th>
            </tr>
            <tr>

            </tr>
             <tr>

            </tr>
            <tr>
                <th>amount paid</th>                
                <th>{{ totalPirce() | currency }}</th>               
            </tr>
            </table>
          </div>
    </div>
</div>   

<script>angular.module('CartApp', [])
    .controller('CartController', function ($scope) {
        $scope.isShow=false;
        $scope.checkout=function(){
            $scope.isShow=true;
        }
    $scope.items = [
          {title: 'Double King', quantity: 0,price: 100.40}, 
          {title: 'Double Queen',quantity: 0,price: 90.40}, 
          {title: 'Single Queen',quantity: 0,price: 80.82}
          ];
    $scope.remove = function (index) {
        $scope.items.splice(index, 1);
    };
    $scope.totalPirce = function () {
        var total = 0;
        angular.forEach($scope.items, function (value, key) {
            total += value.quantity * value.price;
        });
        return total;
    };
    $scope.totalRoom = function () {
        var total = 0;
        angular.forEach($scope.items, function (value, key) {
            total +=value.quantity;
        });
        return total;
    };     

});     
    </script>   

</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要在POST调用中发送这些值,POST的接收者将根据该页面显示新页面,甚至将数据存储在数据库中。

How to make an HTTP POST request in node.js?