looking for some solutions? You are welcome.

SOLVED: Why do I get an error when I serve a static file for two routes in node.js using sendFile() method

Sairam:

I tried to send a static html file for two routes '/' and '/test'. It is working for '/' route but not '/test'/

I am getting following error:

TypeError: path must be absolute or specify root to res.sendFile
    at ServerResponse.sendFile (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\response.js:421:11)
    at E:\sairam\javascript\node\Node middleware\index.js:11:9
    at Layer.handle [as handle_request] (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\layer.js:95:5)
    at E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\index.js:335:12)
    at next (E:\sairam\javascript\node\Node middleware\node_modules\express\lib\router\index.js:275:10)
    at SendStream.error (E:\sairam\javascript\node\Node middleware\node_modules\serve-static\index.js:121:7)

This is my app.js

let express = require('express');
let app = express();

app.use(express.static('public'));

app.get('/', function(req,res){
    res.sendFile('index.html');
    res.end();
})
app.get('/test', function(req,res){
    res.sendFile('index.html');
    res.end();
})
app.listen(3000);

index.html file is in public folder.. that was am serving as static container.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent