res.sendFile不是Node.js函数

res.sendFile is not a function Node.js

本文关键字:js 函数 Node 不是 sendFile res      更新时间:2023-09-26

我无法使用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程序

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

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

sendFile仅在Express模块中。

试试这个代码

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

对Toanalien的(正确的)答案进行Piggybacking,您可以通过以下方式完成:

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.

我也遇到了同样的问题,这对我来说很有效。希望它能起作用。

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

上面的答案是正确的,我只是添加了一个没有express.js但有route调度器的工作示例。

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

则主叫端点CCD_ 5将返回CCD_。

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

试试这个兄弟

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);
});
app.get("/", function(req,res){
    res.sendFile(__dirname + "/html filename with .html extension");
});

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

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

在您的系统中安装express,在您的终端[ubuntu]中使用以下内容

npm install express

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

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

然后我删除了"错误"的论点,它非常有效。像这样:

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

我已经做了一些测试,主要问题是回调必须有两个参数(req,res)=>{},试图在不抛出错误的情况下实现代码,当然express必须作为依赖项安装。你给参数起的名字并不重要,但如果你必须只使用一个参数,那将被解释为req-