【问题标题】:res.sendFile is not a function Node.jsres.sendFile 不是 Node.js 的函数
【发布时间】:2015-12-10 05:20:49
【问题描述】:

我无法使用 node.js 发送 HTML 文件

所以首先这是我遇到的错误

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)

我的 app.js 代码是

var http = require('http');

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

如果我遗漏了一些简单的东西,我很抱歉,因为这是我制作的第一个 node.js 程序

【问题讨论】:

  • 我认为sendFile 是一种ExpressJS 响应方法。您需要安装和使用 Express 才能使用它。

标签: javascript node.js iis


【解决方案1】:

这个具体问题已经得到解答,但值得一提的是,如果您使用的是“express”版本 3.x,修复可能就像将 res.sendFile('path-to-file'); 切换到 res.sendfile('path-to-file'); 一样简单

这就是我的问题。因此,您可以升级 express 版本(或)将方法名称更改为小写以解决此问题。

【讨论】:

  • 这救了我的命:D
  • 又救了一条人命!
  • 还有一个。谢谢 :)
  • 是的,让我登录,以便我可以为您的答案投票。 ;-) 谢谢!
【解决方案2】:

sendFile 仅在 Express 模块中。

试试这个代码

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

【讨论】:

  • 请注意,直到您安装了 Express。
  • 啊,我的回答应该只是对你的评论。干得好,先生或女士。
  • 问题是 Express 似乎没有安装,尽管我以为我已经安装了
  • @DarkN3ss 先安装 Express。 npm install express --save
【解决方案3】:

捎带 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  

http.ServerResponsefs.createReadStream

【讨论】:

  • 谢谢。也解决了我的问题。
【解决方案4】:

我遇到了同样的问题,这对我有用。 希望它会起作用。

 function(req,res){};
  • 第一个参数应该是“req”*

【讨论】:

    【解决方案5】:

    上面的答案是正确的,我只是添加没有express.jsroutedispatcher的工作示例。

    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);
    

    调用端点/test 将返回views/test.html

     package.json 是,

    {                                                                                                                                                                                 
      "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"                                                                                 
      }                                                                                                    
    }
    

    【讨论】:

      【解决方案6】:
      app.get("/", function(req,res){
          res.sendFile(__dirname + "/html filename with .html extension");
      });
      

      检查函数中的第一个参数必须是req然后是res,确保你也这样做了,因为有时我们会因为一些小错误而出错。

      这是我个人的经验,我已经尝试了所有的解决方案,但是当我检查我的代码时,我发现我已经将 res 作为第一个参数,将 req 作为第二个参数。

      【讨论】:

      • 这也是我的情况。感谢您的提醒!
      【解决方案7】:

      试试这个兄弟们

      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);
      });
      

      【讨论】:

        【解决方案8】:

        在您的终端 [ubuntu] 中使用以下命令在您的系统中安装 express

        npm install express
        

        【讨论】:

          【解决方案9】:

          在我的例子中,发生这种情况是因为函数需要两个参数,但我这样分配了 3:

          app.use(function notFoundHandler(err, req, res) {
              res.sendFile('404.html');
          })
          

          然后我删除了 'err' 参数,它运行良好。像这样:

          app.use(function notFoundHandler(req, res) {
              res.sendFile('404.html');
          })
          

          【讨论】:

            猜你喜欢
            • 2018-03-19
            • 2018-10-13
            • 2016-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-02
            • 2018-11-26
            • 1970-01-01
            相关资源
            最近更新 更多