Absolute path res.sendFile

If i do

res.sendfile('public/index1.html'); 

then I get a warning about the server console

express deprecated res.sendfile : use res.sendfile instead

but it works great on the client side.

But when I change it to

 res.sendFile('public/index1.html'); 

I get an error

TypeError: the path must be absolute or point root to res.sendfile

and index1.html not displayed.

I can not understand what the absolute path is. I have a public directory at the same level as server.js . I am making res.sendfile from server.js . I also declared app.use(express.static(path.join(__dirname, 'public')));

Adding a directory structure:

 /Users/sj/test/ ....app/ ........models/ ....public/ ........index1.html 

What is the absolute path to point here?

I am using Express 4.x.

+114
path express
Aug 23 '14 at 15:20
source share
11 answers

The express.static is separate from res.sendFile , so initializing it in the absolute way in your public directory will not do anything with res.sendFile . You must use the absolute path directly with res.sendFile . There are two easy ways:

  • res.sendFile(path.join(__dirname, '../public', 'index1.html'));
  • res.sendFile('index1.html', { root: path.join(__dirname, '../public') });

Note. __dirname returns the directory where the current executable script is located. In your case, it looks like server.js is in app/ . So, to get to public , you first need to go back one level: ../public/index1.html .

Note. path is a built-in module that must be require d for the above code to work: var path = require('path');

+265
Aug 23 '14 at 16:26
source share

Try instead:

 res.sendFile('public/index1.html' , { root : __dirname}); 

It worked for me. root: __ dirname takes the address where server.js is in the above example, and then to go to index1.html (in this case), the returned path is to get the directory where the shared folder is located.

+41
Jul 24 '15 at 17:42
source share
 res.sendFile( __dirname + "/public/" + "index1.html" ); 

where __dirname will control the name of the directory where the current script is located ( server.js ).

+8
Nov 20 '15 at 8:35
source share

An alternative that is not yet listed, which was used for me, simply uses path.resolve with separate lines or with only one 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/4648/

+6
May 26 '17 at 16:49
source share

I tried this and it worked.

 app.get('/', function (req, res) { res.sendFile('public/index.html', { root: __dirname }); }); 
+4
Jan 12 '17 at 12:03 on
source share

Another way to do this is by writing less code.

 app.use(express.static('public')); app.get('/', function(req, res) { res.sendFile('index.html'); }); 
+3
Aug 14 '16 at 22:29
source share

Based on other answers, this is a simple example of how to fulfill 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) 

It also doubles as an easy way to respond to index.html for every request , because I use an asterisk * to intercept all files that were not found in your static (public) directory; which is the most common use case for web applications. Change to / to return the index in the root path only.

+3
Aug 31 '18 at 0:46
source share

process.cwd() returns the absolute path of your project.

Then:

 res.sendFile( `${process.cwd()}/public/index1.html` ); 
+2
02 Sep '17 at 17:48 on
source share

If you want to set it up once and use it everywhere, just set up your middleware. When you configure your application, use the following to define a new function for 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"); }); 
0
Aug 27 '18 at 0:33
source share

I am using Node.Js and I had the same problem ... I decided to just add '/' at the beginning of each script and a link to a static CSS file.

Before:

 <link rel="stylesheet" href="assets/css/bootstrap/bootstrap.min.css"> 

After:

 <link rel="stylesheet" href="/assets/css/bootstrap/bootstrap.min.css"> 
0
Feb 10 '19 at 20:12
source share

You can use send instead of sendFile so as not to encounter an error! it will help you!

 fs.readFile('public/index1.html',(err,data)=>{ if(err){ consol.log(err); }else { res.setHeader('Content-Type', 'application/pdf'); 

for telling the browser that your answer is a PDF type

 res.setHeader('Content-Disposition', 'attachment; filename='your_file_name_for_client.pdf'); 

if you want this file to open immediately on the same page after the user has downloaded it, write "inline" instead of the attachment in the code above.

 res.send(data) 
0
Jul 21 '19 at 9:13
source share