-
Notifications
You must be signed in to change notification settings - Fork 28
/
server.js
36 lines (30 loc) · 1.02 KB
/
server.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
const express = require('express');
const path = require('path');
const app = express();
const mongoose = require('mongoose');
// fetches root files from client/build
//app.use(express.static(path.join(__dirname, 'client', 'build')));
// May only be exist once in app
mongoose.connect("mongodb://my_user:my_pwd@localhost:27017/mern", { useNewUrlParser: true });
const Schema = mongoose.Schema;
const memberSchema = new Schema({
firstName: String,
lastName: String
});
const Member = mongoose.model("member", memberSchema);
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.get('/favicon.ico', (req, res) => {
res.sendFile(path.join(`${__dirname}/favicon.ico`));
});
app.get('/members', (req, res) => {
Member.find({}, "firstName lastName").then(members => {
if (members !== null && members.length > 0) {
res.write(JSON.stringify(members));
} else {
res.write("No members found");
}
res.end();
});
}).listen(8000);