This tutorial is heavily based on a Scotch.io tutorial. Go there later for more awesome stuff.
touch server.js
mkdir app && cd app
mkdir models && cd models
touch link.js
cd ../..
npm init
Add to package.json
"dependencies": {
"express": "~4.0.0",
"mongoose": "~3.6.13",
"body-parser": "~1.0.1"
}
then run npm install
server.js should be:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
router.get('/', function(req, res) {
res.json({ message: 'Hello world!' });
});
app.use('/api', router);
app.listen(port);
console.log('Server running on port ' + port);
Visit localhost:8080/api in a browser to see "Hello world!"
Open Postman and send a GET request to http://localhost:8080/api/
Add to server.js
var mongoose = require('mongoose');
mongoose.connect('');
Create a database at Mongolab
Add a user to the database
Paste database connection info into server.js
Add to ./app/models/link.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var LinkSchema = new Schema({
url: String,
description: String
});
module.exports = mongoose.model('Link', LinkSchema);
Add to server.js
var Link = require('./app/models/link');
Add to server.js
router.use(function(req, res, next) {
console.log('Something happened.');
next();
});
Good time to talk about callback functions and async nature of Node.
Send GET in browser and Postman to see logging.
Add to server.js
router.route('/links')
.post(function(req, res) {
var link = new Link();
link.url = req.body.url;
link.description = req.body.description;
link.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Link ' + link.url + ' created.' });
});
})
Use Postman to create a few links.
Visit Mongolab to see the collection exists.
Add to server.js withing the /links
route as another verb
.get(function(req, res) {
Link.find(function(err, links) {
if (err)
res.send(err);
res.json(links)
})
});
Add to server.js
router.route('/links/:link_id')
.get(function(req, res) {
Link.findById(req.params.link_id, function(err, link) {
if (err)
res.send(err);
res.json(link);
});
});
Send a GET to a specific _ID value and get a response.
Add to server.js while also removing the semicolon to properly chain
.put(function(req, res) {
Link.findById(req.params.link_id, function(err, link) {
if (err)
res.send(err);
link.url = req.body.url;
link.description = req.body.description;
link.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Link updated!' });
});
});
});
Now we'll send a PUT request to change the url and description.
Then use a GET request to confirm the update succeeded.
Let's make our PUT fail by introducing a typo and then debug. Good to write tests and error handlers.
Add to server.js while removing semicolon to allow for proper chaining
.delete(function(req, res) {
Link.remove({
_id: req.params.link_id
}, function(err, link) {
if (err)
res.send(err);
res.json({ message: 'Deleted.' });
});
});
Send the DELETE request.
Send a GET to the same endpoint.
Send a GET to the /links endpoint to see you still have links but not the one you deleted.