Skip to content

Commit

Permalink
examples: use path.join instead of concatenation
Browse files Browse the repository at this point in the history
closes #3236
  • Loading branch information
chainhelen authored and dougwilson committed Mar 6, 2017
1 parent 67168fe commit 8eb95ae
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 31 deletions.
3 changes: 2 additions & 1 deletion examples/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
var express = require('../..');
var bodyParser = require('body-parser');
var hash = require('pbkdf2-password')()
var path = require('path');
var session = require('express-session');

var app = module.exports = express();

// config

app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

// middleware

Expand Down
6 changes: 3 additions & 3 deletions examples/downloads/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var express = require('../../');
var path = require('path');
var app = module.exports = express();

app.get('/', function(req, res){
Expand All @@ -16,10 +17,9 @@ app.get('/', function(req, res){
// /files/* is accessed via req.params[0]
// but here we name it :file
app.get('/files/:file(*)', function(req, res, next){
var file = req.params.file;
var path = __dirname + '/files/' + file;
var filePath = path.join(__dirname, 'files', req.params.file);

res.download(path, function(err){
res.download(filePath, function (err) {
if (!err) return; // file sent
if (err && err.status !== 404) return next(err); // non-404 error
// file for download not found
Expand Down
3 changes: 2 additions & 1 deletion examples/ejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var express = require('../../');
var path = require('path');

var app = module.exports = express();

Expand All @@ -21,7 +22,7 @@ app.engine('.html', require('ejs').__express);

// Optional since express defaults to CWD/views

app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

// Without this you would need to
// supply the extension to res.render()
Expand Down
3 changes: 2 additions & 1 deletion examples/error-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/

var express = require('../../');
var path = require('path');
var app = module.exports = express();
var logger = require('morgan');
var silent = 'test' == process.env.NODE_ENV;

// general config
app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// our custom "verbose errors" setting
Expand Down
5 changes: 3 additions & 2 deletions examples/jade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
*/

var express = require('../../lib/express');
var path = require('path');

// Path to our public directory

var pub = __dirname + '/public';
var pub = path.join(__dirname, 'public');

// setup middleware

Expand All @@ -15,7 +16,7 @@ app.use(express.static(pub));

// Optional since express defaults to CWD/views

app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

// Set our default template engine to "jade"
// which prevents the need for extensions
Expand Down
3 changes: 2 additions & 1 deletion examples/markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var escapeHtml = require('escape-html');
var express = require('../..');
var fs = require('fs');
var marked = require('marked');
var path = require('path');

var app = module.exports = express();

Expand All @@ -21,7 +22,7 @@ app.engine('md', function(path, options, fn){
});
});

app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

// make it the default so we dont need .md
app.set('view engine', 'md');
Expand Down
5 changes: 3 additions & 2 deletions examples/mvc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

var express = require('../..');
var logger = require('morgan');
var path = require('path');
var session = require('express-session');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
Expand All @@ -17,7 +18,7 @@ var app = module.exports = express();
app.set('view engine', 'jade');

// set views for error and 404 pages
app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

// define a custom res.message() method
// which stores messages in the session
Expand All @@ -34,7 +35,7 @@ app.response.message = function(msg){
if (!module.parent) app.use(logger('dev'));

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

// session support
app.use(session({
Expand Down
35 changes: 19 additions & 16 deletions examples/mvc/lib/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@

var express = require('../../..');
var fs = require('fs');
var path = require('path');

module.exports = function(parent, options){
var dir = path.join(__dirname, '..', 'controllers');
var verbose = options.verbose;
fs.readdirSync(__dirname + '/../controllers').forEach(function(name){
if (!fs.statSync(__dirname + '/../controllers/' + name).isDirectory()) return;
fs.readdirSync(dir).forEach(function(name){
var file = path.join(dir, name)
if (!fs.statSync(file).isDirectory()) return;
verbose && console.log('\n %s:', name);
var obj = require('./../controllers/' + name);
var obj = require(file);
var name = obj.name || name;
var prefix = obj.prefix || '';
var app = express();
var handler;
var method;
var path;
var url;

// allow specifying the view engine
if (obj.engine) app.set('view engine', obj.engine);
app.set('views', __dirname + '/../controllers/' + name + '/views');
app.set('views', path.join(__dirname, '..', 'controllers', name, 'views'));

// generate routes based
// on the exported methods
Expand All @@ -31,27 +34,27 @@ module.exports = function(parent, options){
switch (key) {
case 'show':
method = 'get';
path = '/' + name + '/:' + name + '_id';
url = '/' + name + '/:' + name + '_id';
break;
case 'list':
method = 'get';
path = '/' + name + 's';
url = '/' + name + 's';
break;
case 'edit':
method = 'get';
path = '/' + name + '/:' + name + '_id/edit';
url = '/' + name + '/:' + name + '_id/edit';
break;
case 'update':
method = 'put';
path = '/' + name + '/:' + name + '_id';
url = '/' + name + '/:' + name + '_id';
break;
case 'create':
method = 'post';
path = '/' + name;
url = '/' + name;
break;
case 'index':
method = 'get';
path = '/';
url = '/';
break;
default:
/* istanbul ignore next */
Expand All @@ -60,15 +63,15 @@ module.exports = function(parent, options){

// setup
handler = obj[key];
path = prefix + path;
url = prefix + url;

// before middleware support
if (obj.before) {
app[method](path, obj.before, handler);
verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), path, key);
app[method](url, obj.before, handler);
verbose && console.log(' %s %s -> before -> %s', method.toUpperCase(), url, key);
} else {
app[method](path, handler);
verbose && console.log(' %s %s -> %s', method.toUpperCase(), path, key);
app[method](url, handler);
verbose && console.log(' %s %s -> %s', method.toUpperCase(), url, key);
}
}

Expand Down
5 changes: 3 additions & 2 deletions examples/route-separation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var express = require('../..');
var path = require('path');
var app = express();
var logger = require('morgan');
var cookieParser = require('cookie-parser');
Expand All @@ -17,7 +18,7 @@ module.exports = app;
// Config

app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.set('views', path.join(__dirname, 'views'));

/* istanbul ignore next */
if (!module.parent) {
Expand All @@ -27,7 +28,7 @@ if (!module.parent) {
app.use(methodOverride('_method'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));

// General

Expand Down
5 changes: 3 additions & 2 deletions examples/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

var express = require('../..');
var path = require('path');
var redis = require('redis');

var db = redis.createClient();
Expand All @@ -15,7 +16,7 @@ var db = redis.createClient();

var app = express();

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

// populate search

Expand Down Expand Up @@ -45,7 +46,7 @@ app.get('/search/:query?', function(req, res){
*/

app.get('/client.js', function(req, res){
res.sendFile(__dirname + '/client.js');
res.sendFile(path.join(__dirname, 'client.js'));
});

/* istanbul ignore next */
Expand Down

0 comments on commit 8eb95ae

Please sign in to comment.