PROGRAMOWANIE JAVASCRIPT

Programowanie JavaScript 11

HTTP

HTTP

REST JSON API

  • REST - REpresentational State Transfer
  • JSON - JavaScript Object Notation
  • API - Application Programming Interface

REST

mkdir http_server
cd http_server
npm init
npm install express --save
npm install body-parser --save
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/', (req, res) => {
  res.send('Hey!')
})

app.get('/app1', (req, res) => {
  res.send('hello from app1')
})

app.get('/app2', (req, res) => {
  res.send('hello from app2')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

Firewall

tcp vs udp

źródło: www.colocationamerica.com

mkdir app1
cd app1
npm init
npm install express --save
npm install body-parser --save
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3001

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/app1', (req, res) => {
  res.send('Hey! From APP1')
})

app.get('/app1/test', (req, res) => {
  res.send('Hey! From APP1[test]')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

proxy

const httpProxy = require('http-proxy');
const apiProxy = httpProxy.createProxyServer();
const app1Server = 'http://localhost:3001'
app.all("/app1*", function(req, res) {
  console.log('proxing to app1Server')
  apiProxy.web(req, res, {
    target: app1Server
  })
})

Zadanie

  • utworzyć “endpoint”, który będzie pobieral informacje o gistcie/gistach przy uzyciu GithubWrapper-a