【问题标题】:AngularJS - How to disable directory browsing/listing when rendering views with Http web serverAngularJS - 如何在使用 Http Web 服务器呈现视图时禁用目录浏览/列表
【发布时间】:2015-10-26 17:57:18
【问题描述】:

我的 RESTful MEAN 应用程序有一个用于后端的 node.js 服务器和一个用于前端的 HTTP 服务器。以下是项目结构:

当我从控制台运行 My Directory/app/HTTP-server 并进行浏览时,其显示页面如下所示,并带有目录列表/浏览:

一旦我点击页面呈现的视图。以下是我的 app.js:

'use strict';

var app = angular.module('app', ['ngRoute', 'authControllers', 'authServices']);
var authControllers = angular.module('authControllers', []);
var authServices = angular.module('authServices', []);

var options = {};
options.api = {};
//dev URL
options.api.base_url = "http://localhost:3000";

app.config(['$locationProvider', '$routeProvider',
function($location, $routeProvider) {
  $routeProvider.
  when('/', {
    templateUrl: 'partials/home.html',
    controller: 'authCtrl'
  }).
  when('/login', {
    templateUrl: 'partials/signin.html',
    controller: 'authCtrl'
  }).
  when('/register', {
    templateUrl: 'partials/signup.html',
    controller: 'authCtrl'
  }).
  when('/me', {
    templateUrl: 'partials/me.html',
    controller: 'authCtrl'
  }).
  otherwise({
    redirectTo: '/'
  });
}]);
app.config(['$httpProvider', function ($httpProvider) {
  $httpProvider.interceptors.push('TokenInterceptor');
}]);
app.run(function($rootScope, $location, $window, AuthenticationService) {
  $rootScope.$on("$routeChangeStart", function(event, nextRoute, currentRoute) {
    //redirect only if both isAuthenticated is false and no token is set
    if (nextRoute != null && nextRoute.access != null && nextRoute.access.requiredAuthentication
      && !AuthenticationService.isAuthenticated && !$window.sessionStorage.token) {
        $location.path("/login");
      }
    });
  });

server.js:

var express = require('express'),
jwt = require('express-jwt'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('express-error-handler'),
tokenManager = require('./server/config/token_manager'),
secret = require('./server/config/secret'),
http = require('http'),
path = require('path');

var app = module.exports = express();

app.set('port', process.env.PORT || 3000);    
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'app')));

var routes = require('./server/routes');
routes.users = require('./server/routes/users.js');
var env = process.env.NODE_ENV || 'development';

// development only
if (env === 'development') {
  app.use(errorHandler());
}

// production only
if (env === 'production') {
  // TODO
}

app.all('*', function(req, res, next) {
  res.set('Access-Control-Allow-Origin', 'http://localhost');
  res.set('Access-Control-Allow-Credentials', true);
  res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');
  res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Authorization');
  if ('OPTIONS' == req.method) return res.send(200);
  next();
});

/*
Login
*/
app.post('/login', routes.users.login);
app.post('/user/register', routes.users.register);
app.get('/me', routes.users.me);

/*
Logout
*/
app.get('/logout', jwt({secret: secret.secretToken}), routes.users.logout);

process.on('uncaughtException', function(err) {
    console.log(err);
});

/**
* Start Server
*/
http.createServer(app).listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

需要做什么来禁用此目录列表/浏览或仅在http://localhost:8080/ 上呈现我的视图?

【问题讨论】:

  • 你能显示你的 server.js 文件内容吗?
  • @Salem:用 server.js 代码修改了问题。

标签: javascript angularjs node.js httpserver


【解决方案1】:

在您的server.js 更改

express.static(path.join(__dirname, 'app'));

express.static(path.join(__dirname, 'app'), {index: false});

【讨论】:

  • 其实修改后还是显示目录浏览
猜你喜欢
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多