I'm following a tutorial on the MEAN stack, but it's a bit old and some of the methods the author uses are deprecated. He uses sendfile() and I changed it to sendFile() because the server was giving me warnings about sendfile() being deprecated. The new sendFile() says it takes an absolute path as opposed to a relative one. I had an endpoint which worked:
app.get('/', function (req, res){
res.sendFile(__dirname + 'layouts/posts.html')
})
in the server, but now we're breaking all the endpoints out into controllers. The current (relevant) file structure looks like this:
/controllers/static.js
/layouts/posts.html
The tutorial says the endpoint in /controllers is supposed to look like this:
var router = require('express').Router()
router.get('/', function (req, res){
res.sendFile('layouts/posts.html')
})
module.exports = router
with this corresponding code in the server:
app.use(require('./controllers/static'))
With ('layouts/post.html') I get the error "path must be absolute or specify root to res.sendFile" and when I try to add __dirname or a path from the server root, I get an ENOENT ...../controllers/somethingItried/posts.html. Can someone explain the best way to fix this and what the server is considering the absolute path here? My attempts to make a path from root have failed:
../layouts/posts.html
as well as the suggestions from this page:
node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]