Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions controllers/apiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,61 @@ const fs = require('fs');
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser')
const MongoClient = require('mongodb').MongoClient;

router.use(bodyParser.json());
const ObjectID = require('mongodb').ObjectID;

router.get('/posts', (req, res, next) => {
const filePath = __dirname + '/../data/posts.json';
const callbackFunction = function(error, file) {
if(error) {
return next(error);
}
// we call .toString() to turn the file buffer to a String
const fileData = file.toString();
// we use JSON.parse to get an object out the String
const postsJson = JSON.parse(fileData);
res.json(postsJson);
};
fs.readFile(filePath, callbackFunction);


router.get('/students', (req, res) => {
const mongoConnection = 'mongodb://localhost:27017/profile';
MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('students').find();
cursor.toArray((error, students) => {
db.close();
res.json(students);
});
});
});

router.get('/students/:id', (req, res) => {
const studentId = req.params.id;
const mongoConnection = 'mongodb://localhost:27017/profile';
MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('students').find(ObjectID(studentId));
cursor.toArray((error, students) => {
db.close();
res.json(students);
});
});
});

router.post('/posts', (req, res) => {
console.log(req.body);
res.status(500).send('not implemented');
router.get('/posts/:id', (req, res,next) => {
//const studentId = req.params.id;
const postId = req.params.id;
const mongoConnection = 'mongodb://localhost:27017/profile';
MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('posts').find(ObjectID(postId));
cursor.toArray((error, posts) => {
db.close();
res.json(posts);
});
});
});

router.get('/posts/', (req, res,next) => {

const mongoConnection = 'mongodb://localhost:27017/profile';
MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('posts').find();
cursor.toArray((error, posts) => {
db.close();
res.json(posts);
});
});
});



module.exports = router;
31 changes: 15 additions & 16 deletions controllers/siteController.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
const fs = require('fs');
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;

router.get('/', function (req, res) {
const filePath = __dirname + '/../data/posts.json';
const callbackFunction = function(error, file) {
if(error) {
return next(error);
}
// we call .toString() to turn the file buffer to a String
const fileData = file.toString();
// we use JSON.parse to get an object out the String
const postsJson = JSON.parse(fileData);
// send the json to the Template to render
res.render('index', {
title: "Michael's profile",
subheading: "A modern Website built in Node with Handlebars",
posts: postsJson
// Write code to connect to database and return posts
const mongoConnection = 'mongodb://localhost:27017/profile';

MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('posts').find({});
cursor.toArray((error, posts) => {
db.close();
// res.json(posts);
res.render('index', {
title: "Michael's profile",
subheading: "A modern Website built in Node with Handlebars",
posts: posts
});
});
};
fs.readFile(filePath, callbackFunction);
});
});


Expand Down
19 changes: 0 additions & 19 deletions data/posts.json

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"dependencies": {
"body-parser": "^1.17.2",
"express": "^4.15.4",
"express-handlebars": "^3.0.0"
"express-handlebars": "^3.0.0",
"mongodb": "^2.2.31"
},
"devDependencies": {
"nodemon": "^1.11.0"
Expand Down