Permalink
Browse files

Fix no-path-concat

  • Loading branch information...
1 parent 937e041 commit 954d0c7eeae0549b451a4c8f3242538597cc3c82 @bananaappletw bananaappletw committed Oct 20, 2016
Showing with 10 additions and 9 deletions.
  1. +2 −1 blog/lib/render.js
  2. +1 −1 stream-file/README.md
  3. +4 −4 stream-file/app.js
  4. +2 −2 templates/app.js
  5. +1 −1 upload/app.js
View
@@ -4,10 +4,11 @@
*/
var views = require('co-views');
+var path = require('path');
// setup views mapping .html
// to the swig template engine
-module.exports = views(__dirname + '/../views', {
+module.exports = views(path.join(__dirname, '/../views'), {
map: { html: 'swig' }
});
@@ -13,6 +13,6 @@ To see results:
1. The stat() function at the bottom of app.js returns another function that will call the normal fs.stat() to get information about the named file. (The function stat() is a promise - it will ultimately return a value, although it may take a while.)
2. When any program *yields* to a function, it pauses while that function proceeds asynchronously, and eventually returns a value. When the function returns, the program resumes at that point.
-3. In the example, app.use() starts everything off with `fstat = yield stat(path)`. We say it "yields to the stat() function." That is, app.use() pauses while the stat() function begins to execute (asynchronously), and the node interpreter goes off to work on other tasks. When the fs.stat() call completes and returns a value, app.use() resumes, and sets the value of `fstat` to the value returned by stat().
+3. In the example, app.use() starts everything off with `fstat = yield stat(fpath)`. We say it "yields to the stat() function." That is, app.use() pauses while the stat() function begins to execute (asynchronously), and the node interpreter goes off to work on other tasks. When the fs.stat() call completes and returns a value, app.use() resumes, and sets the value of `fstat` to the value returned by stat().
4. This example also uses the createReadStream() function to create a stream which is another way to handle data (asynchronously) from a file.
5. `this.body` gets the result of the fs.createReadStream() (which is the stream's data) and sends it to the web browser client that has connected in to the URL above.
View
@@ -7,12 +7,12 @@ var extname = path.extname;
// try GET /app.js
app.use(function *() {
- var path = __dirname + this.path;
- var fstat = yield stat(path);
+ var fpath = path.join(__dirname, this.path);
+ var fstat = yield stat(fpath);
if (fstat.isFile()) {
- this.type = extname(path);
- this.body = fs.createReadStream(path);
+ this.type = extname(fpath);
+ this.body = fs.createReadStream(fpath);
}
});
View
@@ -1,12 +1,12 @@
-
+var path = require('path');
var views = require('co-views');
var koa = require('koa');
var app = module.exports = koa();
// setup views, appending .ejs
// when no extname is given to render()
-var render = views(__dirname + '/views', { ext: 'ejs' });
+var render = views(path.join(__dirname, '/views'), { ext: 'ejs' });
// dummy data
View
@@ -26,7 +26,7 @@ app.use(function *(next) {
// serve files from ./public
-app.use(serve(__dirname + '/public'));
+app.use(serve(path.join(__dirname, '/public')));
// handle uploads

0 comments on commit 954d0c7

Please sign in to comment.