【问题标题】:Full stack javascript routing explanation needed需要全栈 javascript 路由解释
【发布时间】:2015-06-17 01:45:41
【问题描述】:

我已经使用 yeoman 和 Angular Fullstack 生成器搭建了一个完整堆栈的 Mongo、Express、Angular、Node 应用程序

它创建了一个 server/app.js 文件,该文件执行一个 routes.js 来处理由 express 服务器提供的资源。

routes.js 的内容如下:

  // Insert routes below
  app.use('/api/things', require('./api/thing'));
  app.use('/api/users', require('./api/user'));

  app.use('/auth', require('./auth'));

  // All undefined asset or api routes should return a 404
  app.route('/:url(api|auth|components|app|bower_components|assets)/*')
   .get(errors[404]);

  // All other routes should redirect to the index.html
  app.route('/*')
    .get(function(req, res) {
      res.sendfile(app.get('appPath') + '/index.html');
    });

我的问题是如何将 index.html 以外的任何文件提供给浏览器。我已经测试过了,例如文件“http://localhost:9000/assets/images/yeoman.png”确实会返回到浏览器。但是怎么做?根据我在 routes.js 中阅读的内容,对该 png 的请求应该返回 index.html

的文本

我对此有点困惑,非常感谢您的解释。

谢谢!

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    如果你进入 config/express.js 你会看到这样的:

        app.use(express.static(path.join(config.root, 'public')));
    

    这应该是不言自明的。

    UPD。您可以使用路由覆盖特定文件的此行为(如果您确实需要的话)。

    【讨论】:

    • 谢谢,config/express.js 文件是我遗漏的一部分。现在有道理了:-)
    猜你喜欢
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多