无法获得路线角

时间:2017-02-04 22:56:26

标签: angularjs http templates get routing

我无法打开“/ d”路线,但“/”正在工作。可能是什么问题呢?我尝试了不同的东西但是id找不到解决方案。

    var myApp = angular.module('myApp',['ngRoute']);

myApp.config(function ($routeProvider) {
    $routeProvider.when('/', {
        controller: 'OrdersController',
        templateUrl: 'views/lp.html'

})

   .when('/d',{
            controller:'OrdersController',
            templateUrl: 'views/order_detail.html'
        })



        .otherwise({
            redirectTo: '/'
        });


});

服务器端代码如下所示。也许他们不喜欢彼此;)

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

app.use(express.static(__dirname+'/client'));
app.use(bodyParser.json());

Order = require('./models/order.js');

//Connect to Mongoose
mongoose.connect('mongodb://localhost/groundlevel');
var db = mongoose.connection;

app.get('/', function(req, res){

    res.send('Please use /api/order');
});

app.get('/api/orders', function (req, res) {
    Order.getOrders(function (err, orders) {
        if(err){
            throw err;
        }
        res.json(orders);
    })

});

app.get('/api/orders/:_id',function (req, res) {
    Order.getOrderById(req.params._id, function (err, order) {
    if(err){
        throw err;
    }
    res.json(order);
})
});


app.post('/api/orders', function (req, res) {
    var order = req.body;
    Order.addOrder(order, function (err, order) {
        if(err){
            throw err;
        }
        res.json(order);
    })

});



app.listen(3000);
console.log('Running on Port 3000...');

1 个答案:

答案 0 :(得分:1)

让客户端应用程序处理路由。在您的服务器端执行以下操作

var path= require('path');
// All other routes should redirect to the index.html
app.route('/*')
  .get((req, res) => {
    res.sendFile(path.resolve(__dirname + '/client/index.html'));
});