-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
49 lines (38 loc) · 1.02 KB
/
server.js
File metadata and controls
49 lines (38 loc) · 1.02 KB
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
39
40
41
42
43
44
45
46
47
48
49
var path = require('path');
var express = require('express');
var app = express();
var port = 8080;
var router = express.Router();
var mongoose = require("mongoose");
mongoose.Promise = global.Promise;
mongoose.connect("mongodb://127.0.0.1/players");
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/'));
app.get('*', (req, res) =>{
res.sendFile(path.resolve(__dirname, 'src/index.html'));
});
app.listen(port, () => {
console.log("Server listening on port " + port);
});
var userSchema = new mongoose.Schema({
username: String,
WPM: Number,
date: {
type: Date,
default: Date.now
}
});
var User = mongoose.model("User", userSchema);
app.post("/addname", (req, res) => {
console.log("hello");
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");
});
});