node.js - Nodejs 和 Angularjs,清理 url

标签 node.js angularjs mod-rewrite

如何设置 node/angular 让 index.html/appexample.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/

相关文章:

javascript - 确定 Meteor/Node 中服务器返回的输出是客户端上的 stderr 还是 stdout?

javascript - 使用 groupBy 进行多次 ng-repeat

apache - Htaccess 重定向命令不区分大小写

node.js - Node Elasticsearch 批量索引失败且无提示

node.js - Reactjs 和 Mongodb 连接

javascript - Angularjs ng-value for boolean in select 不工作

javascript - Angular 负载定制 Controller

php - 使用 mod_rewrite 将所有流量重定向到 index.php

WordPress Symfony 同居

mysql - 序列化范围类型和解析器