如何设置 node/angular 让 index.html/app
在 example.com
而不是 example.com/app/index 运行。 html#/home
我尝试将 index.html
放在 Node 服务器的根目录下,但仍然将 url 保留为 example.com/index.html#/home
最佳答案
您需要启用html5mode
。可以找到它的文档和注意事项 here .
这是取自 Brian Ford 的示例:
angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives']).
config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
// configure $routeProvider, then...
$locationProvider.html5Mode(true);
}
]);
angular-ui 框架有 example configuration将其连接到 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
在 Brian 的文章中,您不必手动映射静态 Assets 文件夹,因为他的示例提供了单个 index.html
,将 partials 映射到 /partials/:name
,然后与/api/*
关于node.js - Nodejs 和 Angularjs,清理 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027896/