-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
46 lines (38 loc) · 935 Bytes
/
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
37
38
39
40
41
42
43
44
45
46
const express = require('express');
const mongoose = require('mongoose');
const app = express();
//Retrieve Data Models
Users = require('./models/users');
Posts = require('./models/posts');
// Connect to Mongoose
mongoose.connect('mongodb://localhost/sparkit');
var db = mongoose.connection;
app.get('/', (req, res) => {
res.send('Please use /api/users or /api/posts');
});
app.get('/api/users', (req, res) => {
Users.getUsers(function(err, users){
if (err) {
throw err;
}
res.json(users);
});
});
app.get('/api/posts', (req, res) => {
Posts.getPosts(function(err, posts){
if (err) {
throw err;
}
res.json(posts);
});
});
app.get('/api/posts/:email', (req, res) => {
Posts.getPostsByEmail(req.params.email, function(err, posts){
if (err) {
throw err;
}
res.json(posts);
});
});
const port = 8080;
app.listen(port, () => `Server running on port ${port}`);