只有在我的角度控制器和服务中添加CRUD后,当我访问localhost:8080 / somePage时才会出现404错误。
无法获取/某些页面
导航栏中指向同一网址的href链接仍在使用!
<li><a href="/somePage">somePage</a></li>
有些管理页面不会有页面链接,所以我需要直接找到它们。有什么建议吗?
直接通过localhost查看API:8080 / api / stuff会显示json:
[{ “text”:“item 1”, “完成”:假, “ id”:“53402c4390dfad962a000001”, “ _v”:0}]
这是我的appRoutes.js
angular.module('appRoutes', []).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
// home page
.when('/', {
templateUrl: 'views/home.html',
controller: 'mainController'
})
//
.when('/somePage', {
templateUrl: 'views/somePage.html',
controller: 'mainController'
});
$locationProvider.html5Mode(true);
}]);
这是节点+ express server.js:
// set up ======================================================================
var express = require('express');
var app = express(); // create our app w/ express
var mongoose = require('mongoose'); // mongoose for mongodb
var port = process.env.PORT || 8080; // set the port
var database = require('./config/database'); // load the database config
// configuration ===============================================================
mongoose.connect(database.url); // connect to mongoDB database on modulus.io
app.configure(function() {
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
app.use(express.logger('dev')); // log every request to the console
app.use(express.bodyParser()); // pull information from html in POST
app.use(express.methodOverride()); // simulate DELETE and PUT
});
// routes ======================================================================
require('./app/routes.js')(app); // load the routes
// listen (start app with node server.js) ======================================
app.listen(port);
console.log("App listening on port " + port);
根据Iqbal Fauzi的评论,这里是Node.js routes.js ..
// load the stuff model
var Stuff = require('./models/stuff');
// expose the routes to our app with module.exports
module.exports = function(app) {
// api ---------------------------------------------------------------------
// get all the stuff
app.get('/api/stuff', function(req, res) {
// use mongoose to get all the stuff in the database
Stuff.find(function(err, stuff) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
res.json(stuff); // return all the stuff in JSON format
});
});
// create stuff and send back all the stuff after creation
app.post('/api/stuff', function(req, res) {
// create stuff, information comes from AJAX request from Angular
Stuff.create({
text : req.body.text,
done : false
}, function(err, stuff) {
if (err)
res.send(err);
// get and return all the stuff after you create another
Stuff.find(function(err, stuff) {
if (err)
res.send(err)
res.json(stuff);
});
});
});
// delete stuff
app.delete('/api/stuff/:stuff_id', function(req, res) {
Stuff.remove({
_id : req.params.stuff_id
}, function(err, stuff) {
if (err)
res.send(err);
// get and return all the stuff after you create another
Stuff.find(function(err, stuff) {
if (err)
res.send(err)
res.json(stuff);
});
});
});
// application -------------------------------------------------------------
app.get('/', function (req, res) {
res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});
// commented out for testing this problem.. because this otherwise redirects..
// app.get('*', function (req, res) {
// res.redirect('/'); // redirect to / and index.html will be served
// });
};
答案 0 :(得分:1)
取消注释app.get('*', function (req, res)
,而不是将其重定向到&#39; /&#39;您最好返回index.html
文件,让AngularJS为您处理浏览器URL。
app.get('*', function (req, res) {
res.sendfile('./public/index.html');
});