-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
38 lines (32 loc) · 1015 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// import variables
var express = require("express");
var app = express();
var port = 3000;
// body-parser variables
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// this kind of body parser shoudn't be used anymore. it's going to be deppreciated
// mogodb variables
var mongoose = require("mongoose");
var nameSchema = new mongoose.Schema({
firstName: String,
lastName: String
});
var User = mongoose.model("User", nameSchema);
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://localhost:27017/node-demo");
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("item saved to database");
})
.catch(err =>{
res.status(400).send("unable to save to database");
});
});
app.listen(port, ()=> {console.log("Server listening on port " + port)});