user683016
user683016

Reputation: 51

Error by using res.sendfile() in nodejs

i m having project on nodejs, im begginer in nodejs. when i try to use this

app.get('/', function(req, res) res.sendfile('index.html');});

its tells me to use sendFile( calital F). And When I use capital F in sendFile, its shows me this error

TypeError: path must be absolute or specify root to res.sendFile

   at ServerResponse.sendFile 

What should I do anybode help me

Upvotes: 0

Views: 440

Answers (4)

Onkar Dighe
Onkar Dighe

Reputation: 17

if you have 'home/../public/index.html' these kind of paths then you've to use

const path = require('path');
.
.
.

app.get('/', async(req, res)=>{
    res.sendFile(path.resolve(__dirname+'/../public/index.html'));
})

Upvotes: 0

rikg93
rikg93

Reputation: 1279

Try this, it works for me!

        var path = require('path');
        res.sendFile(path.resolve('output.xlsx'));

Bye!

Upvotes: 0

Amit Adhikari
Amit Adhikari

Reputation: 740

You should enter the exact filepath of the file in your system.

Add this to the top of the file:

var path = require('path');

Then:

app.get('/', function(req, res){ 
    console.log(path.join(__dirname, 'index.html')); //See the output in console
    res.sendFile(path.join(__dirname, 'index.html')); //this joins your current directory and filename, giving you the full path to the file.
});

I don't know why it's not working for you. Just writing 'index.html' usually works. But this will definitely work.

Upvotes: 0

Pankaj kumar Panigrahi
Pankaj kumar Panigrahi

Reputation: 761

Can you try running as sudo ?

sudo npm install nodejs

better way to install node is to download the installers https://nodejs.org/en/download/

Upvotes: 1

Related Questions