我在活动时遇到Backbone.history.start({pushState: true});
的问题
我使用骨干路由器'example/:id':'test'
,浏览器返回错误
获取myhost:1337 / example / js / views / test.js 404(未找到)
我希望使用Backbone进行轮换,例如myhost:1337/example/test
,而无需请求nodejs。
si,我不知道为什么,
可能是我的服务器Nodejs? 或者可能是我的代码,它写得不好? 提前致谢
我的服务器代码是
//var http = require('http');
var path = require('path'),
express = require('express'),
routes = require('./routes/index'),
http = require('http'),
app = require('express')();
app.configure(function(){
//app.set('view options',{layout: false });
app.set('port', process.env.PORT || 1337);
app.use(express.bodyParser()),
app.use(express.static(path.join(__dirname, 'public')));
app.use(app.router); // you need this line so the .get etc. routes are run and if an error within, then the error is parsed to the ned middleware (your error reporter)
app.use(function(err, req, res, next) {
if(!err) return next(); // you also need this line
console.log("error!!!");
res.send("error!!!");
});
});
app.get('*',function(req,res){
res.setHeader('content-type', 'text/javascript');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.redirect('http://localhost:1337#'+req.url);
});
http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});
答案 0 :(得分:0)
这适用于您的服务器。当你输入你的路线时,我愿意打赌你没有按下shift键,所以你的服务器里有这样的东西
app.get('example?:id':'test', function() {});
当你应该:
app.get('example/:id':'test', function() {});
这个区块:
app.post('/addPlace?:?',routes.addPlace);
app.get('/searchPlace?:q',routes.searchPlace);
app.post('/showPlace',routes.showPlace);
app.get('/showPlaceById?:id',routes.showPlaceById)
app.post('/deletePlace?:?',routes.deletePlace);
到处查看?
?这应该是:
app.post('/addPlace',routes.addPlace);
app.get('/searchPlace/:q',routes.searchPlace);
app.post('/showPlace',routes.showPlace);
app.get('/showPlaceById/:id',routes.showPlaceById)
app.post('/deletePlace',routes.deletePlace);
如果你改变了,/showPlaceById/:id
将返回你的期望。