Nodejs&Angularjs,清洁url
如何设置节点/angular度以使index.html/app
在example.com
运行,而不是在example.com/app/index.html#/home
我试图把index.html
放在节点服务器的根目录下,但是仍然把url作为example.com/index.html#/home
你需要的是启用html5mode
。 这是文件和考虑可以在这里find。
以下是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框架具有示例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的文章中,您不必手动映射静态资产文件夹,因为他的示例提供了单个index.html
,将partials映射到/partials/:name
,然后与/api/*
进行交互
如果您在NODEJS服务器上使用Yoeman或grunt构build。
您可以简单地将新文件server.js
添加到根文件夹中,并且如果您的应用程序文件夹在里面并且是您的根文件夹。
var express = require('express'); var app = express(); app.use(express.static(__dirname + "/app")); app.all('*', function(req, res, next) { // Just send the index.html for other files to support HTML5Mode res.sendFile('/app/index.html', { root: __dirname }); }); var port = 1337; app.listen(port); //the port you want to use console.log('Server running at localhost:' + port);