Sendfile noworking in express


#1

I’m a beginner in node and express. I’m trying to create a node application where I’m trying to load an HTML file once client connected to then server successfully. below is my source code.

var http = require(‘http’);
var express = require(‘express’);
var application = express();
var path = require(‘path’);
var server = require(‘http’).createServer(application);

application.get(’/’, function(req, res){
res.sendfile(path.resolve(’/test.html’));
});

server.listen(process.env.PORT);

When I run the app. I got below mentioned error. I ave
"Error: ENOENT: no such file or directory, stat '/test.html’
at Error (native)"

Kindly help me.


#2

Can you try changing res.sendfile(path.resolve('/test.html')); to res.sendfile(path.resolve(__dirname, 'test.html'));? That should add the rest of the path before test.html, and correctly point to the file.