简体   繁体   English

节点/快速 - 服务静态不工作/如何使用服务静态文件的路由

[英]Node / Express - Serve-static not working / How to use routes with serve-static files

I'm having some trouble with routing and serving static files. 我在路由和提供静态文件方面遇到了一些麻烦。

My directory structure is like this: 我的目录结构是这样的:

app/
--server.js
--controllers/
--directives/
--etc.
assets/
--img/
--css/
--etc.
views/
--partials/
  --view1.jade
  --view2.jade
--index.jade
--layout.jade
--etc.

I'd like this to happen: 我希望这件事发生:

  • app / - Serve static files (for example, "GET /app/controllers/controller.js" should send the literal file) app / - 提供静态文件(例如,“GET /app/controllers/controller.js”应该发送文字文件)
  • assets / - Serve static files (for example, "GET /assets/css/custom.css" should send the literal file) assets / - 提供静态文件(例如,“GET /assets/css/custom.css”应该发送文字文件)
  • views / - Dynamic routing (for example, "GET /partials/view1" uses the routing rules I have set up) views / - 动态路由(例如,“GET / partials / view1”使用我设置的路由规则)

This is how I've set up my server: 这就是我设置服务器的方式:

  ...
//PATHS:
  var viewsPath = path.join(__dirname, '..', 'views');
  var appPath = path.join(__dirname, '..', 'app');
  var assetsPath = path.join(__dirname, '..', 'assets');
  ...
//SERVE-STATIC:
  app.set('views', viewsPath);
  app.use(serveStatic(appPath));
  app.use(serveStatic(assetsPath));
  ...
//ROUTES:
  // serve index and view partials
  app.get('/', routes.index);

  //Partials
  app.get('/partials/:name', routes.partials);
 ...

But this is what happens when I visit the index: 但是当我访问索引时会发生这种情况:

[.../app]$ node app.server.js 
**Node Version:  v0.12.2
**Express Version:  4.12.3
Path to views:  /myproject/views
Static serving:  /myproject/app
Static serving:  /myproject/assets
Express server listening on port 3000
GET / 304 765.837 ms - -
GET /assets/css/app.css 404 9.851 ms - 31
GET /assets/css/bootstrap.min.css 404 1.837 ms - 41
GET /assets/css/bootstrap-theme.min.css 404 0.569 ms - 47
GET /assets/css/custom.css 404 0.787 ms - 34
GET /assets/libs/angular/angular.min.js 404 0.458 ms - 47
GET /app/app.modules.js 404 0.639 ms - 31
GET /app/services/services.js 404 0.560 ms - 37
GET /app/controllers/controllers.js 404 1.474 ms - 43
GET /app/filters/filters.js 404 0.662 ms - 35
GET /app/directives/directives.js 404 1.853 ms - 41
GET /assets/js/jquery.min.js 404 3.824 ms - 36
GET /assets/js/bootstrap.min.js 404 0.804 ms - 39
GET /assets/libs/angular/angular-bootstrap.min.js 404 0.624 ms - 57
GET /assets/libs/angular/angular-bootstrap-prettify.min.js 404 0.721 ms - 66
GET /assets/img/logo.png 404 0.465 ms - 39
GET /favicon.ico 404 1.216 ms - 24

The " views " part works , but the " assets " and " app " parts don't work . views ”部分有效 ,但“ 资产 ”和“ app ”部分不起作用

How do I set up the "serve-static" part to work? 如何设置“服务静态”部分才能工作? Do I still need a route even-though it's listed to serve static files? 我是否仍然需要一条路线 - 虽然它被列为提供静态文件? Would I still need to set up a route and do "sendFile"? 我还需要设置路线并执行“sendFile”吗?

Thanks! 谢谢!

/////////////////////////// ///////////////////////////

Update: 更新:

/////////////////////////// ///////////////////////////

I tried this: 我试过这个:

  ...
//PATHS:
  var viewsPath = path.join(__dirname, '..', 'views');
  var appPath = path.join(__dirname, '..', 'app');
  var assetsPath = path.join(__dirname, '..', 'assets');
  ...
//SERVE-STATIC:
  app.set('views', viewsPath);
  app.use(serveStatic('app', appPath)); //<--- Changed
  app.use(serveStatic('assets', assetsPath)); //<--- Changed
  ...
//ROUTES:
  // serve index and view partials
  app.get('/', routes.index);

  //Partials
  app.get('/partials/:name', routes.partials);
 ...

And also renamed "app.modules.js" to "app-modules.js" but didn't work either :/ Still getting lots of 404's: 并且还将“app.modules.js”重命名为“app-modules.js”但是也没有工作:/仍然有很多404:

GET / 200 645.235 ms - 1867
GET /assets/css/app.css 404 9.209 ms - 31
GET /assets/css/bootstrap.min.css 404 2.609 ms - 41
GET /assets/css/bootstrap-theme.min.css 404 1.297 ms - 47

To get the behavior you want, change this: 要获得所需的行为,请更改此设置:

app.use(serveStatic(appPath));

...to this: ......对此:

  app.use('/app', serveStatic(appPath));

The earlier code specifies serving the contents of your app directory statically, but it does not include the app part of the path. 较早的代码指定静态地提供app目录的内容,但它不包括路径的app部分。 It basically means "Treat everything in appPath as if it was docroot." 它基本上意味着“将appPath所有appPath视为docroot”。 So, to get (eg) app/server.js served in your web browser, you would just go to http://example.com/server.js and not http://example.com/app/server.js . 因此,要在您的网络浏览器中提供(例如) app/server.js ,您只需访问http://example.com/server.js不是 http://example.com/app/server.js By specifying the path in app.use() , you get the behavior you want. 通过在app.use()指定路径,您可以获得所需的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM