Cant get Route angular

2019-03-03 13:40发布

I cant get open the route "/d" but "/" is working. What could be the problem? I tried different things but id did not find a solution.

    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: '/'
        });


});

Server side code looks like this. Maybe they dont like each other ;)

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条回答
地球回转人心会变
2楼-- · 2019-03-03 13:56

To let your client side application handle the routing. In your server side do the following

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'));
});
查看更多
登录 后发表回答