From c721392c7361cb3905fd0ee453d98bad9ef5b465 Mon Sep 17 00:00:00 2001 From: hamza Date: Sat, 30 Jul 2016 20:49:35 +0100 Subject: [PATCH 1/2] add Node's path module --- examples/static-files/index.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/examples/static-files/index.js b/examples/static-files/index.js index c3b1659d861..46ad4f42ce5 100644 --- a/examples/static-files/index.js +++ b/examples/static-files/index.js @@ -3,9 +3,15 @@ */ var express = require('../..'); +var path = require('path'); var logger = require('morgan'); var app = express(); +//Set up the paths +var publicPath = path.resolve(__dirname,'public'); +var cssPath = path.resolve(__dirname,'public/css'); +var jsPath = path.resolve(__dirname,'public/js'); + // log requests app.use(logger('dev')); @@ -16,7 +22,7 @@ app.use(logger('dev')); // that you pass it. In this case "GET /js/app.js" // will look for "./public/js/app.js". -app.use(express.static(__dirname + '/public')); +app.use(express.static(publicPath)); // if you wanted to "prefix" you may use // the mounting feature of Connect, for example @@ -24,13 +30,17 @@ app.use(express.static(__dirname + '/public')); // The mount-path "/static" is simply removed before // passing control to the express.static() middleware, // thus it serves the file correctly by ignoring "/static" -app.use('/static', express.static(__dirname + '/public')); +app.use('/static', express.static(publicPath)); // if for some reason you want to serve files from // several directories, you can use express.static() // multiple times! Here we're passing "./public/css", // this will allow "GET /style.css" instead of "GET /css/style.css": -app.use(express.static(__dirname + '/public/css')); +app.use(express.static(cssPath)); + +//Here we're passing "./public/js", +// this will allow "GET /app.js" instead of "GET /js/app.js": +app.use(express.static(jsPath)); app.listen(3000); console.log('listening on port 3000'); From 956501e4a701e6e6276fbb64f926e744aa01d23b Mon Sep 17 00:00:00 2001 From: hamza Date: Sun, 31 Jul 2016 11:15:09 +0100 Subject: [PATCH 2/2] add path.join --- examples/static-files/index.js | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/examples/static-files/index.js b/examples/static-files/index.js index 46ad4f42ce5..04ad8693546 100644 --- a/examples/static-files/index.js +++ b/examples/static-files/index.js @@ -7,11 +7,6 @@ var path = require('path'); var logger = require('morgan'); var app = express(); -//Set up the paths -var publicPath = path.resolve(__dirname,'public'); -var cssPath = path.resolve(__dirname,'public/css'); -var jsPath = path.resolve(__dirname,'public/js'); - // log requests app.use(logger('dev')); @@ -22,7 +17,7 @@ app.use(logger('dev')); // that you pass it. In this case "GET /js/app.js" // will look for "./public/js/app.js". -app.use(express.static(publicPath)); +app.use(express.static(path.join(__dirname, 'public'))); // if you wanted to "prefix" you may use // the mounting feature of Connect, for example @@ -30,21 +25,17 @@ app.use(express.static(publicPath)); // The mount-path "/static" is simply removed before // passing control to the express.static() middleware, // thus it serves the file correctly by ignoring "/static" -app.use('/static', express.static(publicPath)); +app.use('/static', express.static(path.join(__dirname, 'public'))); // if for some reason you want to serve files from // several directories, you can use express.static() // multiple times! Here we're passing "./public/css", // this will allow "GET /style.css" instead of "GET /css/style.css": -app.use(express.static(cssPath)); - -//Here we're passing "./public/js", -// this will allow "GET /app.js" instead of "GET /js/app.js": -app.use(express.static(jsPath)); +app.use(express.static(path.join(__dirname, 'public', 'css'))); app.listen(3000); console.log('listening on port 3000'); console.log('try:'); console.log(' GET /hello.txt'); console.log(' GET /js/app.js'); -console.log(' GET /css/style.css'); +console.log(' GET /css/style.css'); \ No newline at end of file