Nodejs&Angularjs,清洁网址

Kir*_*ngo 4 mod-rewrite node.js angularjs

如何设置节点/角度以使index.html/app运行example.com而不是example.com/app/index.html#/home

我试着把它放在index.html节点服务器的根目录但是仍然保留了urlexample.com/index.html#/home

Jim*_*ert 9

你需要的是启用html5mode.它的文档和注意事项可以在这里找到.

以下是Brian Ford的一个例子:

angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    // configure $routeProvider, then...
    $locationProvider.html5Mode(true);
  }
]);
Run Code Online (Sandbox Code Playgroud)

angular-ui框架有示例配置,用于连接express.js:

var express = require('express');
var app = express();

app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));

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

app.listen(3006); //the port you want to use
Run Code Online (Sandbox Code Playgroud)

在Brian的文章中,您不必手动映射静态资产文件夹,因为他的示例将单个index.html映射部分映射到/partials/:name,然后与之交互/api/*