-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (32 loc) · 844 Bytes
/
index.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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const messages = require('./db/messages');
const app = express();
app.use(morgan('tiny'));
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.json({
message: 'Behold The MEVN Stack!'
});
});
app.get('/messages', (req, res) => {
messages.getAll().then((messages) => {
res.json(messages);
});
});
app.post('/messages', (req, res) => {
console.log(req.body);
messages.create(req.body).then((message) => {
res.json(message);
}).catch((error) => {
res.status(500);
res.json(error);
});
});
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`listening on ${port}`);
});