-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
41 lines (34 loc) · 882 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
39
40
41
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
//Bring the model into app.js file
const Post = require('./Model/Post.model');
//Acquire mongodb database
const db = 'mongodb://localhost/mymongodb';
mongoose.connect(db);
// use body parser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
const port = 9090;
app.get('/', function(req, res) {
res.send('<h3>App is running!</h3>');
});
//Create another Route for posts
app.get('/posts', function(req, res) {
console.log('All posts');
Post.find({})
.exec(function(err, posts) {
if(err) {
res.send('error occured')
} else {
console.log(posts);
res.json(posts);
}
});
});
app.listen(port, function (){
console.log(`Listening on port ${port}!`);
});