If I do a
res.sendfile('public/index1.html'); then I get a server console warning
express deprecated
res.sendfile: Useres.sendFileinstead
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.htmlWhat is the absolute path to be specified here ?
I'm using Express 4.x.
212 Answers
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 required for the above code to work: var path = require('path');
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.
1An 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
res.sendFile( __dirname + "/public/" + "index1.html" );where __dirname will manage the name of the directory that the currently executing script ( server.js ) resides in.
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.
I tried this and it worked.
app.get('/', function (req, res) { res.sendFile('public/index.html', { root: __dirname });
}); process.cwd() returns the absolute path of your project.
Then :
res.sendFile( `${process.cwd()}/public/index1.html` ); 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) Another way to do this by writing less code.
app.use(express.static('public'));
app.get('/', function(req, res) { res.sendFile('index.html');
}); 1 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");
}); 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"> 1 The following worked for me
res.sendFile(path.resolve(__dirname,'./path_to_file_from_current_directory'));