Javascript res.sendFile 不是 Node.js 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34194245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
res.sendFile is not a function Node.js
提问by jLynx
I am not able to send a HTML file using node.js
我无法使用 node.js 发送 HTML 文件
So first off this is the error I am getting
所以首先这是我得到的错误
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 is
我的 app.js 代码是
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 as this is the first node.js program I have made
如果我遗漏了一些简单的东西,我很抱歉,因为这是我制作的第一个 node.js 程序
回答by lift-it-luke
This specific issue has already been answered, but it's worth mentioning that if you're using "express" version 3.x, the fix could be as easy as switching res.sendFile('path-to-file');
to res.sendfile('path-to-file');
这个特定问题已经得到解答,但值得一提的是,如果您使用的是“express”版本 3.x,则修复可能就像切换res.sendFile('path-to-file');
到res.sendfile('path-to-file');
This was the problem in my case. So you can either upgrade the express version (or) change the method name with lower case to fix this issue.
这就是我的问题。因此,您可以升级快速版本(或)更改小写的方法名称以解决此问题。
回答by Toanalien
sendFile only in Express module.
sendFile 仅在 Express 模块中。
Try this code
试试这个代码
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.sendFile('path-to-file');
});
app.listen(PORT);
回答by Fissure King
Piggybacking on Toanalien's (correct) answer, you could accomplish the same by:
借助 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
回答by prayagupd
The answer above is correct, I'm just adding working example with no express.js
but route
dispatcher.
上面的答案是正确的,我只是添加了没有express.js
但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);
calling endpoint /test
will return the views/test.html
.
调用端点/test
将返回views/test.html
.
?package.jsonis,
? 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"
}
}
回答by Mehran Ahemad
I had the same problem, this worked for me. Hope it will work.
我有同样的问题,这对我有用。希望它会起作用。
function(req,res){};
- first argument should be "req" *
- 第一个参数应该是“req”*
回答by Tanmay Rajgor
app.get("/", function(req,res){
res.sendFile(__dirname + "/html filename with .html extension");
});
Checkout that in a function first parameter must be req and then res, make sure that you have done the same, because sometimes we get an error just because of some small mistakes.
检查在函数中第一个参数必须是 req 然后是 res,确保你已经做了同样的事情,因为有时我们会因为一些小错误而出错。
This is my personal experience, I have tried all the solutions, but when I checked my code, then I observed that i have written res as a first argument and req as a second argument.
这是我的个人经验,我尝试了所有解决方案,但是当我检查我的代码时,我发现我将 res 作为第一个参数,将 req 作为第二个参数。
回答by naeem1098
In my case, it happened because the function was requiring two arguments but I assigned 3 like this:
就我而言,发生这种情况是因为该函数需要两个参数,但我像这样分配了 3 个:
app.use(function notFoundHandler(err, req, res) {
res.sendFile('404.html');
})
Then I removed the 'err' argument and it worked perfectly. like this:
然后我删除了 'err' 参数,它工作得很好。像这样:
app.use(function notFoundHandler(req, res) {
res.sendFile('404.html');
})
回答by Alexander Mills
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);
});
回答by Manna
install express in your system using following in your terminal [ubuntu]
在您的终端 [ubuntu] 中使用以下命令在您的系统中安装 express
npm install express