James Sherry PRO
Web Development Tutor and Co-Founder of { The Jump } Digital School
How is this possible??
HTTP(S) - HyperText Transfer Protocol (Secure)
FTP - File Transfer Protocol
SMTP - Simple Mail Transfer Protocol (href="mailto: ...")
GIT
WS - Web Sockets
SSH - Secure Shell
TEL - Telephone Service (href="tel: ...")
SMS - Short Messaging Service (href="sms: ...")
skype, telnet, IRC, etc. etc.
Agreed Ways of Doing Things
[
{
name: 'James',
age: 40,
isDeveloper: true
}
]
[
{
"name": "James",
"age": 40,
"isDeveloper": true
}
]
stringify
parse
Headers are information the transportation layer needs to know about, not your application.
Headers contain 'meta' information about the call itself:
Headers can be the standard ones or you can make your own custom ones.
GET authors ⇠ Authors::index()
POST authors ⇠ Authors::post()
DELETE authors/{id} ⇠ Authors::delete()
GET authors/{id} ⇠ Authors::get()
PUT authors/{id} ⇠ Authors::put()
var express = require('express');
var app = express();
app.listen(3333, function(){
console.log('Server is listening');
});
To run this file, go to your command line and do:
npm start
(in your server.js)
var express = require('express');
var app = express();
app.use(express.static('public'));
app.listen(3333, function(){
console.log('Server is listening');
});
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/cars', function(request, response){
var cars = [{name: 'ferarri'}, {name: 'lamborghini'}];
return res.json(cars);
});
app.listen(3333, function(){
console.log('Server is listening');
});
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static('public'));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
app.get('/cars', function(request, response){
var cars = [{name: 'ferarri'}, {name: 'lamborghini'}];
return res.json(cars);
});
app.post('/cars', function(request, response){
return res.json(req.body);
});
app.listen(3333, function(){
console.log('Server is listening');
});
app.get('/users/:userId/books/:bookId?', function (req, res) { // ? = optional
res.send(req.params)
});
/*
* Route path: /users/:userId/books/:bookId
* Request URL: http://localhost:3000/users/34/books/8989
* req.params: { "userId": "34", "bookId": "8989" }
*/
Query Params:
// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"
Things that allow us to persist data
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose'); // DB control program
var app = express();
app.use(express.static('public'));
mongoose.Promise = global.Promise;
var promise = mongoose.connect('mongodb://localhost/first_servers');
promise.then(function(db) {
console.log('DATABASE CONNECTED!!');
}).catch(function(err){
console.log('CONNECTION ERROR', err);
});
app.listen(3333, function(){
console.log('Server is listening');
});
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var blogSchema = new Schema({
title: String,
author: String,
body: String,
comments: [{ body: String, date: Date }],
date: { type: Date, default: Date.now },
hidden: Boolean,
meta: {
votes: Number,
favs: Number
}
});
var Blog = mongoose.model('Blog', blogSchema);
api.get('/people', function(req, res, next){
// Person is a model, like Blog
Person.find({
occupation: /host/,
'name.last': 'Ghost',
age: { $gt: 17, $lt: 66 },
likes: { $in: ['vaporizing', 'talking'] }
})
.limit(10)
.sort({ occupation: -1 })
.select({ name: 1, occupation: 1 })
.exec(function(err, people){
return res.json(people);
});
});
api.post('/tutors', function(req, res){
var tutorData = req.body.tutor;
console.log('tutorData', tutorData);
var tutor = new Tutor(tutorData);
console.log('tutor', tutor);
tutor.save(function(err, model){
if (err) {
return res.status(500).send(err);
}
return res.sendStatus(201);
});
});
api.delete('/tutors/:tutorid', function(req, res){
var tutorIdForDeletion = req.params.tutorId;
Tutor.remove({ _id: tutorIdForDeletion }, function (err) {
if (err) return handleError(err);
// removed!
res.sendStatus(204);
});
});
api.put('/tutors/:tutorid', function(req, res){
var tutorId = req.params.tutorId;
Tutor.update({ _id: tutorId }, { age: 40 }, function (err, raw) {
if (err) return handleError(err);
console.log('The raw response from Mongo was ', raw);
return res.sendStatus(200);
});
});
By James Sherry
Focussing on HTTP, Servers and REST