node.js - Node / Express - Serve-static not working / How to use routes with serve-static files -
i'm having trouble routing , serving static files.
my directory structure this:
app/ --server.js --controllers/ --directives/ --etc. assets/ --img/ --css/ --etc. views/ --partials/ --view1.jade --view2.jade --index.jade --layout.jade --etc.
i'd happen:
- app/ - serve static files (for example, "get /app/controllers/controller.js" should send literal file)
- assets/ - serve static files (for example, "get /assets/css/custom.css" should send literal file)
- views/ - dynamic routing (for example, "get /partials/view1" uses routing rules have set up)
this how i've set 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 , view partials app.get('/', routes.index); //partials app.get('/partials/:name', routes.partials); ...
but happens when visit index:
[.../app]$ node app.server.js **node version: v0.12.2 **express version: 4.12.3 path views: /myproject/views static serving: /myproject/app static serving: /myproject/assets express server listening on port 3000 / 304 765.837 ms - - /assets/css/app.css 404 9.851 ms - 31 /assets/css/bootstrap.min.css 404 1.837 ms - 41 /assets/css/bootstrap-theme.min.css 404 0.569 ms - 47 /assets/css/custom.css 404 0.787 ms - 34 /assets/libs/angular/angular.min.js 404 0.458 ms - 47 /app/app.modules.js 404 0.639 ms - 31 /app/services/services.js 404 0.560 ms - 37 /app/controllers/controllers.js 404 1.474 ms - 43 /app/filters/filters.js 404 0.662 ms - 35 /app/directives/directives.js 404 1.853 ms - 41 /assets/js/jquery.min.js 404 3.824 ms - 36 /assets/js/bootstrap.min.js 404 0.804 ms - 39 /assets/libs/angular/angular-bootstrap.min.js 404 0.624 ms - 57 /assets/libs/angular/angular-bootstrap-prettify.min.js 404 0.721 ms - 66 /assets/img/logo.png 404 0.465 ms - 39 /favicon.ico 404 1.216 ms - 24
the "views" part works, "assets" , "app" parts don't work.
how set "serve-static" part work? still need route even-though it's listed serve static files? still need set route , "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 , view partials app.get('/', routes.index); //partials app.get('/partials/:name', routes.partials); ...
and renamed "app.modules.js" "app-modules.js" didn't work either :/ still getting lots of 404's:
get / 200 645.235 ms - 1867 /assets/css/app.css 404 9.209 ms - 31 /assets/css/bootstrap.min.css 404 2.609 ms - 41 /assets/css/bootstrap-theme.min.css 404 1.297 ms - 47
to behavior want, change this:
app.use(servestatic(apppath));
...to this:
app.use('/app', servestatic(apppath));
the earlier code specifies serving contents of app
directory statically, not include app
part of path. means "treat in apppath
if docroot." so, (e.g.) app/server.js
served in web browser, go http://example.com/server.js
, not http://example.com/app/server.js
. specifying path in app.use()
, behavior want.
Comments
Post a Comment