Skip to content

Commit

Permalink
exapmples: use path.join to concat paths
Browse files Browse the repository at this point in the history
closes #3046
  • Loading branch information
hamzaOp authored and blakeembrey committed Jul 31, 2016
1 parent 9375a9a commit 305f982
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/static-files/index.js
Expand Up @@ -4,6 +4,7 @@


var express = require('../..'); var express = require('../..');
var logger = require('morgan'); var logger = require('morgan');
var path = require('path');
var app = express(); var app = express();


// log requests // log requests
Expand All @@ -16,21 +17,21 @@ app.use(logger('dev'));
// that you pass it. In this case "GET /js/app.js" // that you pass it. In this case "GET /js/app.js"
// will look for "./public/js/app.js". // will look for "./public/js/app.js".


app.use(express.static(__dirname + '/public')); app.use(express.static(path.join(__dirname, 'public')));


// if you wanted to "prefix" you may use // if you wanted to "prefix" you may use
// the mounting feature of Connect, for example // the mounting feature of Connect, for example
// "GET /static/js/app.js" instead of "GET /js/app.js". // "GET /static/js/app.js" instead of "GET /js/app.js".
// The mount-path "/static" is simply removed before // The mount-path "/static" is simply removed before
// passing control to the express.static() middleware, // passing control to the express.static() middleware,
// thus it serves the file correctly by ignoring "/static" // thus it serves the file correctly by ignoring "/static"
app.use('/static', express.static(__dirname + '/public')); app.use('/static', express.static(path.join(__dirname, 'public')));


// if for some reason you want to serve files from // if for some reason you want to serve files from
// several directories, you can use express.static() // several directories, you can use express.static()
// multiple times! Here we're passing "./public/css", // multiple times! Here we're passing "./public/css",
// this will allow "GET /style.css" instead of "GET /css/style.css": // this will allow "GET /style.css" instead of "GET /css/style.css":
app.use(express.static(__dirname + '/public/css')); app.use(express.static(path.join(__dirname, 'public', 'css')));


app.listen(3000); app.listen(3000);
console.log('listening on port 3000'); console.log('listening on port 3000');
Expand Down

0 comments on commit 305f982

Please sign in to comment.