Res.sendFile is not a Node.js function

I cannot send HTML file using node.js

So first is the error I get

Application has thrown an uncaught exception and is terminated:
TypeError: res.sendFile is not a function
    at Server.<anonymous> (C:\Program Files\iisnode\www\test\app.js:4:6)
    at emitTwo (events.js:88:13)
    at Server.emit (events.js:173:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:529:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:89:23)

and my app.js code

var http = require('http');

http.createServer(function (req, res) {
    res.sendFile('test.html', { root: __dirname });
}).listen(process.env.PORT);  

If I am missing something simple, I am sorry that this is the first node.js program I made

+4
source share
5 answers

sendFile is only in the Express module.

Try this code

 var express = require('express');
 var app = express();
 app.get('/', function(req, res) {
     res.sendFile('path-to-file');
 });
 app.listen(PORT);
+10
source

This particular problem has already been answered, but it is worth mentioning that if you are using the β€œexpress” version 3.x, the fix can be as simple as switching res.sendFile('path-to-file');tores.sendFile('path-to-file');

. , - () , .

+13

Toanalien (), :

var http = require('http');
var fs = require('fs');
var path = require('path');

http.createServer(function (req, res) {
  // maybe test for existence here using fs.stat

  res.writeHead(200, {"Content-Type": "text/html"});

  fs.createReadStream(path.resolve(__dirname, 'test.html')) 
    .pipe(res);

}).listen(process.env.PORT || '3000'); // provide a default  

See http.ServerResponse and fs.createReadStream .

+3
source

The correct answer: I just add a working example without express.js, but a route dispatcher .

var http = require('http');                                                                         
var Router = require('routes');                                                                                                                                                   
var router = Router();                                                                                 
var fs = require('fs')                                                                         

router.addRoute("GET /test", (req, res, params) => {  
    let file = __dirname + '/views/test.html'                                                     
    res.writeHead(200, {"Content-Type": "text/html"});                                                 
    fs.createReadStream(file).pipe(res);                        
});                                                                                                    

var server = http.createServer((req, res) => {                                                   
 var match = router.match(req.method + ' ' + req.url);                                                 
 if (match) match.fn(req, res, match.params);                                                          
 else {                                                                                                
  res.statusCode = 404;                                                                                
  res.end('not found\n');                                                                              
 }                                                                                                     
}).listen(process.env.PORT || 3000);

the calling endpoint /testwill return views/test.html.

package.json is,

{                                                                                                                                                                                 
  "name": "node-app",                                                                                  
  "version": "1.0.0",                                                                                  
  "description": "node api",                                                                           
  "main": "server.js",                                                                                 
  "scripts": {                                                                                         
    "test": "echo \"Error: no test specified\" && exit 1"                                              
  },                                                                                                   
  "author": "prayagupd",                                                                               
  "license": "ISC",                                                                                    
  "dependencies": {
    "request": "^2.75.0",                                                                              
    "request-promise": "^4.1.1",                                                                       
    "routes": "^2.1.0"                                                                                 
  }                                                                                                    
}
+1
source

Try this homies

const index = path.resolve(root + '/index.html');
const http = require('http');

const server = http.createServer((req, res) => {
   res.setHeader('Content-Type', 'text/html');
   fs.createReadStream(index).pipe(res);
});
0
source

Source: https://habr.com/ru/post/1619303/


All Articles