jdx / mean-sample

Sample project for Write Modern Web Apps with the MEAN Stack by Jeff Dickey
https://mean-sample.herokuapp.com
166 stars 90 forks source link

Ch. 5 - complications due to use of sendFile() (the recommended express method) #40

Open murshedchoudhury opened 8 years ago

murshedchoudhury commented 8 years ago

As a result of moving over to sendFile(), I had to add dirname to the path :. sendFile(dirname+'...'), to get through the initial few pages of the chapter, otherwise I kept getting the "express deprecated res.sendfile ..." warning.

This was fine until pg. 62, where you separate out the sendfile endpoints. Here, the only workable solution I've found is to add a new object to the static.js file :. var path = require('path') and use its join method to cobble together a usable path for sendFile() :. res.sendFile(path.join(__dirname,'../relative/path/to/post.html').

The method mentioned in the book works fine, but since it is deprecated, I thought I'd try and learn the newer method.

murshedchoudhury commented 8 years ago

Update: decided to look up the api on the express site (http://expressjs.com/4x/api.html#res.sendFile) and found a got a simpler solution. :. var filename = post.html options = { root: './layouts/', headers {'x-timestamp': Date.now(), 'x-sent': true }}

res.sendFile(filename, options, function(err) { ... })

No need for importing a new object - require('path') - or making a convoluted join - res.sendFile(path.join(...)) - much simpler.