Skip to content

dresosh/men-blog2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

###start => mkdir men-blog

  1. cd men-blog/
  2. git init
  3. git add -A
  4. git commit -m "first commit"
  5. git remote add origin https://github.com/username/men-blog-api.git
  6. git push -u origin master
  7. npm init
  8. git add .
  9. git commit -m "packages.json"
  10. git push -u origin master
  11. touch server.js
  12. subl .
  13. npm install mongoose --save
  14. npm install body-parser --save
  15. npm install express --save
  16. mkdir app
  17. cd app
  18. mkdir config
  19. cd config/
  20. touch routes.js
  21. cd ../..
  22. touch .gitignore
  23. touch Procfile
  24. git add .
  25. git commit -m "initial setup + server.js"
  26. git push origin master
  27. git checkout -b routes
  28. cd app
  29. mkdir models controllers
  30. mkdir models controllers
  31. cd models/
  32. touch article.js
  33. cd ..
  34. cd controllers/
  35. touch articles.js
  36. git add .
  37. git commit -m "adds models, controllers"
  38. cd ../..
  39. git add .
  40. git commit -m "builds schema for article"
  41. git checkout master
  42. git merge routes
  43. git push origin master
  44. nodemon
  45. git add -p
  46. git commit - m "continue codealong from here"
  47. git commit -m "continue codealong from here"

Finishing off this morning's setup:

  1. finish routes :

    var express = require('express'),
    	apiRouter = express.Router(),
    	articlesController = require('../controllers/articles'),
    	Article = require('../models/article');
    
    apiRouter.route('/articles')
    	.post(function(req, res){
    		console.log(req.body);
    	})
    
    module.exports = apiRouter;
    
  2. fire up mongo: $ mongod

  3. make a new db: $ mongo > use men-blog // or whatever you named your db

    also if you want to make a change to your package.json you will be able to launch nodemon without sepcifying an entry point.

  4. At this point you should be able to fire up your server $ nodemon || $ nodemon server.js and make a post request to /api/articles and see the req.body be logged out in the server


Let's refactor our .post logic so that it is in the controller:

app/config/routes.js:

```
var express = require('express'),
	apiRouter = express.Router(),
	articlesController = require('../controllers/articles'),
	Article = require('../models/article');

apiRouter.route('/articles')
	.post(articlesController.create);

module.exports = apiRouter;
```

app/controllers/articles.js:

```
//CONTROLLER

function create(req, res) {
	console.log(req.body);
}

module.exports = {
  create: create
};

```

should log you POST


Now lets make this controller ACTUALLY use our Article Schema

//CONTROLLER
var Article = require('../models/article');

function create(req, res) {
	//Instantiate a new article using our awesome Schema:
	var article = new Article(req.body);
	//log it in the console, as before
	console.log(req.body);
	//save the Article / set up error handling
	article.save(function(err) {
		if (err) console.log('not able to create article b/c ' +err);

		res.json({message: 'Article successfully created'});
	})
}

module.exports = {
  create: create
};

men-blog2

men-blog2

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors