Howdy, campers.
I can’t seem to figure out how to get the View/Edit Data button on the index.html page to open the data.html page for the user.
Seems like it should be simple but I have so far been failing over and over again to get this to work! Any assistance is appreciated.
1 Like
DISREGARD, EVERYBODY!! I figured it out.
In the page I am navigating from, I just did this:
<!DOCTYPE html>
<html>
<head>
<title>Intro to Node and MongoDB</title>
</head>
<body>
<h1>Intro 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>
<form action="/goData" method = "get">
<button>View/Edit Data</button>
</form>
</body>
</html>
and then on the main js page I did this:
var express = require("express");
var app = express();
var port = process.env.PORT || 8080;
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var path = process.cwd();
app.use('/controllers', express.static(process.cwd() + '/app/controllers'));
app.use('/public', express.static(process.cwd() + '/public'));
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/myapp");
var nameSchema = new mongoose.Schema({
firstName: String,
lastName: String
});
var User = mongoose.model("User", nameSchema);
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/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.route('/goData')
.get(function (req, res) {
res.sendFile(path + '/public/data.html');
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});