需要完整的堆栈javascript路由说明

时间:2015-06-16 17:37:01

标签: javascript node.js express

我使用带有Angular Fullstack生成器的yeoman搭建了一个完整的堆栈Mongo,Express,Angular,Node应用程序

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

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的文本

我对此感到有点困惑,我真的很感激解释。

谢谢!

1 个答案:

答案 0 :(得分:2)

如果你进入 config / express.js ,你会看到如下内容:

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

哪个应该是不言自明的。

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