res.sendFile命令nodejs中的错误

时间:2018-07-24 02:52:29

标签: node.js mongodb express body-parser

[

var express = require("express");
var app = express();
var port = 3000;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/node-demo");
var nameSchema = new mongoose.Schema({
    firstName: String,
    lastName: String
});
var User = mongoose.model("User", nameSchema);

app.get("/", (req, res) => {
    res.sendFile(__dirname + "/index.html");
});

app.post("/addname", (req, res) => {
    var myData = new User(req.body);
    myData.save()
        .then(item => {
            res.send("Name saved to database");
        })
        .catch(err => {
            res.status(400).send("Unable to save to database");
        });
});

app.listen(port, () => {
    console.log("Server listening on port " + port);
});
<!DOCTYPE html>
<html>
    <head>
        <title>Intro to Node and MongoDB</title>
    </head>

    <body>
        <h1>Into to Node and MongoDB</h1>
        <form method="post" action="/addname">
            <label>Enter Your Name</label><br>
            <input type="text" name="firstName" placeholder="Enter first name..." required>
            <input type="text" name="lastName" placeholder="Enter last name..." required>
            <input type="submit" value="Add Name">
        </form>
    </body>
</html>

]

通过在localhost:3000上运行代码 我在鲍泽身上得到了这个结果

TypeError:res.sendFile不是函数    在app.get(C:\ Users \ DeLL pc \ Documents \ node \ app.js:18:9)    在回调(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ router \ index.js:164:37)    在参数上(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ router \ index.js:138:11)    通过时(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ router \ index.js:145:5)    在Router._dispatch(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ router \ index.js:173:5)    在Object.router [作为句柄](C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ router \ index.js:33:10)    在下一个(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:174:15)    在Object.urlencodedParser [作为句柄](C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ body-parser \ lib \ types \ urlencoded.js:91:7)    在下一个(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:174:15)    在Object.jsonParser [作为句柄](C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ body-parser \ lib \ types \ json.js:110:7)    在下一个(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:174:15)    在Object.expressInit [作为句柄](C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ express \ lib \ middleware.js:30:5)    在下一个(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:174:15)    在Object.query [作为句柄](C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ middleware \ query.js:43:5)    在下一个(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:174:15)    在Function.app.handle(C:\ Users \ DeLL pc \ Documents \ node \ node_modules \ connect \ lib \ proto.js:182:3)

1 个答案:

答案 0 :(得分:0)

我知道让res存在,但res.sendFile()不起作用的唯一原因是,如果您运行的是sendFile方法之前的Express版本,已添加到Express。

来自the doc

  Express v4.8.0及更高版本支持

res.sendFile()。


您还可以添加一些日志记录以获取更多信息:

app.get("/", (req, res) => {
    console.log(typeof res.sendFile);
    console.log(res);
    res.sendFile(__dirname + "/index.html");
});

在控制台上,您可以通过以下命令行查看已安装的Express版本:

npm view express version