2016-04-03 127 views
0

我如何使seo友好的url这样,在后端我有节点服务器。Ango seo友好的网址和页面重新加载问题

http://localhost:3003/my-new-mobile //product 
http://localhost:3003/my-new-category //category 
http://localhost:3003/my-first-blog-post // blog post 

我已经尝试过这样的东西,但它不工作。我将只使用一个控制器的所有4个以上的URL。 我可以使用正规快递吗?

$routeProvider 
     .when('^/([a-z])$', { 
     templateUrl: 'partials/main.html', 
     controller: 'MainCtrl' 
     }) 

当我试图像这样:

<html ng-app="myApp"> 
<base href="/client"> 

和app.js

$locationProvider.html5Mode(true); 

它时重新加载页面显示错误。 http://localhost:3003/my-new-mobile

Cannot GET /my-new-mobile 

任何建议,如何才能做到这一点或Cannot GET /my-new-mobile错误如何避免?

这里是我的server.js

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

app.set('views', __dirname); 
app.engine('html', require('ejs').renderFile); 


var port = process.env.PORT || 3003; 
app.listen(port, function(){ 
     console.log('Listening on port ' + port); 
}); 
    app.get('/', function(req, res){ 
     res.render('client/views/index.html'); 
    }) 

app.get('/partials/:name', function (req, res) { 
    console.log(req.params.name); 
     res.render('client/views/partials/' + req.params.name); 
}); 

app.use('/clientjs', express.static(__dirname + '/client/js')); 
    app.use('/bower_components', express.static(__dirname + '/bower_components')); 
app.use('/images', express.static(__dirname+'/uploads/')); 
+1

服务器必须返回所有与index.html – YOU

回答

1

尝试是这样的 -

$routeProvider 
    .when('/:post', { 
    templateUrl: 'partials/main.html', 
    controller: 'MainCtrl' 
    }) 

你需要设置其他路线以及他们可能会搞砸了,我推荐做这样的事情 -

$routeProvider 
    .when('/posts/:post', { 
    templateUrl: 'partials/main.html', 
    controller: 'MainCtrl' 
    }) 

编辑也许你应该改变你的服务器这样的代码

app.get('/:post', function(req, res){ 
    res.render('client/views/index.html'); 
}) 
+0

你的第一个解决方案是好的,但是当我重新加载页面我得到错误'不能GET /我的新手机' –

+0

如何你在为你的应用程序服务吗 – atefth

+0

http:// localhost:3003/my-new-mobile –

0

您要为每一个路线index.html文件的Express服务器上的不只是/。

对于实例

//Serve Static Assets 
app.use('/images', express.static(__dirname + '/dist/images')); 
app.use('/scripts', express.static(__dirname + '/dist/scripts')); 
app.use('/styles', express.static(__dirname + '/dist/styles')); 
app.use('/favicon.ico', express.static(__dirname + '/dist/favicon.ico')); 
app.use('/robots.txt', express.static(__dirname + '/dist/robots.txt')); 

//Respond to with index for all other routes 
app.get('/*', function(req, res, next) { 
    // Just send the index.html for other files to support HTML5Mode 
    res.sendFile('dist/index.html', { root: __dirname }); 
}); 

这也是值得注意的是,这些都是SEO友好的URL,您的角度应用未SEO没有服务器端渲染友好。你可能想看看prerender.io

+0

让我试试这个溶液 –