res.sendFile absolute path
node.jsExpressPathnode.js Problem Overview
If I do a
res.sendfile('public/index1.html');
then I get a server console warning
> express deprecated res.sendfile
: Use res.sendFile
instead
but it works fine on the client side.
But when I change it to
res.sendFile('public/index1.html');
I get an error
> TypeError: path must be absolute or specify root to res.sendFile
and index1.html
is not rendered.
I am unable to figure out what the absolute path is. I have public
directory at the same level as server.js
. I am doing the res.sendFile
from with server.js
. I have also declared app.use(express.static(path.join(__dirname, 'public')));
Adding my directory structure:
/Users/sj/test/
....app/
........models/
....public/
........index1.html
What is the absolute path to be specified here ?
I'm using Express 4.x.
node.js Solutions
Solution 1 - node.js
The express.static
middleware is separate from res.sendFile
, so initializing it with an absolute path to your public
directory won't do anything to res.sendFile
. You need to use an absolute path directly with res.sendFile
. There are two simple ways to do it:
res.sendFile(path.join(__dirname, '../public', 'index1.html'));
res.sendFile('index1.html', { root: path.join(__dirname, '../public') });
Note: __dirname
returns the directory that the currently executing script is in. In your case, it looks like server.js
is in app/
. So, to get to public
, you'll need back out one level first: ../public/index1.html
.
Note: path
is a built-in module that needs to be require
d for the above code to work: var path = require('path');
Solution 2 - node.js
Just try this instead:
res.sendFile('public/index1.html' , { root : __dirname});
This worked for me. the root:__dirname will take the address where server.js is in the above example and then to get to the index1.html ( in this case) the returned path is to get to the directory where public folder is.
Solution 3 - node.js
An alternative that hasn't been listed yet that worked for me is simply using path.resolve
with either separate strings or just one with the whole path:
// comma separated
app.get('/', function(req, res) {
res.sendFile( path.resolve('src', 'app', 'index.html') );
});
Or
// just one string with the path
app.get('/', function(req, res) {
res.sendFile( path.resolve('src/app/index.html') );
});
(Node v6.10.0)
Idea sourced from https://stackoverflow.com/a/14594282/6189078
Solution 4 - node.js
res.sendFile( __dirname + "/public/" + "index1.html" );
where __dirname
will manage the name of the directory that the currently executing script ( server.js
) resides in.
Solution 5 - node.js
Based on the other answers, this is a simple example of how to accomplish the most common requirement:
const app = express()
app.use(express.static('public')) // relative path of client-side code
app.get('*', function(req, res) {
res.sendFile('index.html', { root: __dirname })
})
app.listen(process.env.PORT)
This also doubles as a simple way to respond with index.html on every request, because I'm using a star *
to catch all files that weren't found in your static (public) directory; which is the most common use case for web-apps. Change to /
to return the index only in the root path.
Solution 6 - node.js
I tried this and it worked.
app.get('/', function (req, res) {
res.sendFile('public/index.html', { root: __dirname });
});
Solution 7 - node.js
process.cwd()
returns the absolute path of your project.
Then :
res.sendFile( `${process.cwd()}/public/index1.html` );
Solution 8 - node.js
Another way to do this by writing less code.
app.use(express.static('public'));
app.get('/', function(req, res) {
res.sendFile('index.html');
});
Solution 9 - node.js
you can use send instead of sendFile so you wont face with error! this works will help you!
fs.readFile('public/index1.html',(err,data)=>{
if(err){
consol.log(err);
}else {
res.setHeader('Content-Type', 'application/pdf');
for telling browser that your response is type of PDF
res.setHeader('Content-Disposition', 'attachment; filename='your_file_name_for_client.pdf');
if you want that file open immediately on the same page after user download it.write 'inline' instead attachment in above code.
res.send(data)
Solution 10 - node.js
If you want to set this up once and use it everywhere, just configure your own middleware. When you are setting up your app, use the following to define a new function on the response object:
app.use((req, res, next) => {
res.show = (name) => {
res.sendFile(`/public/${name}`, {root: __dirname});
};
next();
});
Then use it as follows:
app.get('/demo', (req, res) => {
res.show("index1.html");
});
Solution 11 - node.js
I use Node.Js and had the same problem... I solved just adding a '/' in the beggining of every script and link to an css static file.
Before:
<link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css">
After:
<link rel="stylesheet" href="/assets/css/bootstrap/bootstrap.min.css">
Solution 12 - node.js
The following worked for me
res.sendFile(path.resolve(__dirname,'./path_to_file_from_current_directory'));