res.sendFile 不是 Node.js 的函数

     2023-03-30     67

关键词:

【中文标题】res.sendFile 不是 Node.js 的函数【英文标题】:res.sendFile is not a function 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 才能使用它。 【参考方案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.ServerResponse 和fs.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');
)

【讨论】:

res.sendFile 绝对路径

】res.sendFile绝对路径【英文标题】:res.sendFileabsolutepath【发布时间】:2014-10-1706:37:45【问题描述】:如果我做一个res.sendfile(\'public/index1.html\');然后我收到服务器控制台警告表示不推荐使用res.sendfile:改用res.sendFile但它在客户端... 查看详情

Express res.sendFile 没有上传 CSS

】Expressres.sendFile没有上传CSS【英文标题】:Expressres.sendFileisnotuploadingCSS【发布时间】:2021-11-0908:48:38【问题描述】:代码运行并加载了admin.html文件,但没有加载标题中的CSS文件。只有当我将设计放在HTML中的样式标签之间时它才... 查看详情

Angularjs - TypeError:路径必须是绝对路径或指定根到 res.sendFile

】Angularjs-TypeError:路径必须是绝对路径或指定根到res.sendFile【英文标题】:Angularjs-TypeError:pathmustbeabsoluteorspecifyroottores.sendFile【发布时间】:2015-01-1408:32:16【问题描述】:我正在尝试使用angularjs创建一个联系人应用程序。我在项... 查看详情

Express res.sendfile 抛出禁止错误

】Expressres.sendfile抛出禁止错误【英文标题】:Expressres.sendfilethrowingforbiddenerror【发布时间】:2013-01-1316:27:05【问题描述】:我有这个代码:res.sendfile(\'../../temp/index.html\')但是,它会抛出这个错误:Error:ForbiddenatSendStream.error(/Users/O... 查看详情

无法准确解读错误:TypeError:路径必须是绝对路径或在 ServerResponse.sendFile 处指定 res.sendFile 的根

...解读错误:TypeError:路径必须是绝对路径或在ServerResponse.sendFile处指定res.sendFile的根【英文标题】:Can\'tpreciselydeciphererror:TypeError:pathmustbeabsoluteorspecifyroottores.sendFileatServerResponse.sendFile【发布时间】:2018-03-1819:49:10【问题描述】:... 查看详情

Heroku 上的空白应用程序:express.static 和/或 res.sendFile 问题?

】Heroku上的空白应用程序:express.static和/或res.sendFile问题?【英文标题】:BlankapponHeroku:express.staticandorres.sendFileissue?【发布时间】:2019-01-0406:42:42【问题描述】:我上传了一个React+Node+Express应用到Heroku。该应用程序在本地运行,... 查看详情

如何在使用 res.sendFile() 将请求发送回客户端之前等待文件创建完成

】如何在使用res.sendFile()将请求发送回客户端之前等待文件创建完成【英文标题】:Howtowaitforthefiletogetcreatedbeforesendingrequestbacktoclientusingres.sendFile()【发布时间】:2021-10-2605:51:37【问题描述】:我有一个文件创建器块,它将文件写... 查看详情

下载中带有文件名的NodeJS sendFile

】下载中带有文件名的NodeJSsendFile【英文标题】:NodeJSsendFilewithFileNameindownload【发布时间】:2017-06-1522:53:20【问题描述】:我尝试使用此代码向客户端发送文件:router.get(\'/get/myfile\',function(req,res,next)res.sendFile("/other_file_name.dat"););... 查看详情

使用sendfile()在express中发送静态文件

参考技术AExpresssendFile()方法允许您发送原始文件作为对HTTP请求的响应。您可以将其res.sendFile()视为单个端点的Expressstatic中间件。假设您有一个如下所示的HTML文件index.html:通过将路径传递到index.html,可以使用res.sendFile()使Express... 查看详情

express之sendfile

module.exports=function(req,res,opt){varapplyNo=req.query.applyNo;console.log("applyNo:"+applyNo);varfile=path.resolve(__dirname,"../../web/repay_jld.html");res.sendFile(file);}//页面跳转到repay_jld.html// 查看详情

express:如何使用 sendFile 将 html 和 css 一起发送?

】express:如何使用sendFile将html和css一起发送?【英文标题】:express:howtosendhtmltogetherwithcssusingsendFile?【发布时间】:2016-12-0923:31:46【问题描述】:varapp=require(\'express\')();app.get(\'/\',function(req,res)res.sendFile(__dirname+"/"+"index.h 查看详情

Node.js:node.js 中是不是有同步版本的 `http.get` 方法?

】Node.js:node.js中是不是有同步版本的`http.get`方法?【英文标题】:Node.js:Isthereasynchronousversionofthe`http.get`methodinnode.js?Node.js:node.js中是否有同步版本的`http.get`方法?【发布时间】:2012-03-2113:18:46【问题描述】:node.js中有http.get... 查看详情

node.js 中的嵌套承诺是不是正常?

】node.js中的嵌套承诺是不是正常?【英文标题】:Arenestedpromisesnormalinnode.js?node.js中的嵌套承诺是否正常?【发布时间】:2016-06-1818:03:03【问题描述】:我在学习node.js的过程中一直在努力解决的问题是如何使用node.js进行同步编... 查看详情

TypeError:x 不是 Node.js 中的函数

】TypeError:x不是Node.js中的函数【英文标题】:TypeError:xisnotafunctioninNode.js【发布时间】:2018-11-2613:37:24【问题描述】:我正在开发一个Electron应用程序,我的目标是“拆分”index.js(主进程)文件。目前,我已将与菜单栏相关和... 查看详情

为啥 Node.js 通常使用 'var' 而不是 'let'?

】为啥Node.js通常使用\\\'var\\\'而不是\\\'let\\\'?【英文标题】:WhydoesNode.jscommonlyuse\'var\'insteadof\'let\'?为什么Node.js通常使用\'var\'而不是\'let\'?【发布时间】:2020-01-1622:51:45【问题描述】:基本上我在Node.js上看过的每个教程,甚... 查看详情

当我启动基于 node.js 的站点时,是不是有清单?

】当我启动基于node.js的站点时,是不是有清单?【英文标题】:IsthereachecklistoutthereforwhenIlaunchanode.jsbasedsite?当我启动基于node.js的站点时,是否有清单?【发布时间】:2013-06-0505:15:13【问题描述】:我正在使用node.js完成我的第一... 查看详情

关系数据库是不是不适合 Node.js?

】关系数据库是不是不适合Node.js?【英文标题】:ArerelationaldatabasesapoorfitforNode.js?关系数据库是否不适合Node.js?【发布时间】:2013-08-2422:10:22【问题描述】:最近我一直在玩弄Node.js。在我的特殊情况下,我最终使用了MongoDB,部... 查看详情

Node.js 和 MongoDB 从以前的更新而不是当前的更新中获取数据

】Node.js和MongoDB从以前的更新而不是当前的更新中获取数据【英文标题】:Node.jsandMongoDBgettingdatabackfrompreviousupdateinsteadofcurrentupdate【发布时间】:2022-01-1614:56:36【问题描述】:我正在练习Node.js,并且正在创建一个网站,人们可以... 查看详情