0
votes

[

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>

]

by running the code on localhost:3000 i'm having that result on my bowser

TypeError: res.sendFile is not a function at app.get (C:\Users\DeLL pc\Documents\node\app.js:18:9) at callbacks (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\router\index.js:164:37) at param (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\router\index.js:138:11) at pass (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\router\index.js:173:5) at Object.router [as handle] (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\router\index.js:33:10) at next (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:174:15) at Object.urlencodedParser [as handle] (C:\Users\DeLL pc\Documents\node\node_modules\body-parser\lib\types\urlencoded.js:91:7) at next (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:174:15) at Object.jsonParser [as handle] (C:\Users\DeLL pc\Documents\node\node_modules\body-parser\lib\types\json.js:110:7) at next (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:174:15) at Object.expressInit [as handle] (C:\Users\DeLL pc\Documents\node\node_modules\express\lib\middleware.js:30:5) at next (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:174:15) at Object.query [as handle] (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\middleware\query.js:43:5) at next (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:174:15) at Function.app.handle (C:\Users\DeLL pc\Documents\node\node_modules\connect\lib\proto.js:182:3)

1

1 Answers

0
votes

The only reason I know of for res to exist, but res.sendFile() to not be a function is if you're running an older version of Express from before the sendFile method was added to Express.

From the doc:

res.sendFile() is supported by Express v4.8.0 onwards.


You can also add some logging to get you more info:

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

From your console, you can see what version of Express you have installed with this command line:

npm view express version