done state

This commit is contained in:
Märt Kalmo
2017-05-09 15:42:06 +03:00
commit 18c31430f1
11 changed files with 290 additions and 0 deletions

8
examples/express1.js Normal file
View File

@@ -0,0 +1,8 @@
'use strict';
const express = require('express');
const app = express();
app.use(express.static('./'));
app.listen(3000);

22
examples/express2.js Normal file
View File

@@ -0,0 +1,22 @@
'use strict';
const express = require('express');
const app = express();
app.use(express.static('./'));
app.get('/api/tasks', getTasks);
app.get('/api/tasks/:id', getTask);
app.listen(3000, () => console.log('Server is running...'));
function getTasks(request, response) {
response.set('Content-Type', 'application/json');
response.end(JSON.stringify([{ id: 1 }, { id: 2 }]));
}
function getTask(request, response) {
var id = request.params.id;
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({ id: id }));
}

21
examples/express3.js Normal file
View File

@@ -0,0 +1,21 @@
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.get('/api/tasks', getTasks);
app.post('/api/tasks', saveTask);
app.listen(3000, () => console.log('Server is running...'));
function getTasks(request, response) {
response.json([{ id: 1 }, { id: 2 }]);
}
function saveTask(request, response) {
console.log(request.body); // request should have correct content-type
response.end();
}

37
examples/express4.js Normal file
View File

@@ -0,0 +1,37 @@
'use strict';
const express = require('express');
const app = express();
app.get('/api/p1', p1);
app.get('/api/p2', p2);
app.get('/api/p3', p3);
app.use(errorHandler); // after request handlers
app.listen(3000, () => console.log('Server is running...'));
function p1(request, response) {
try {
throw new Error('p1');
} catch (error) {
response.status(500).send(error.toString());
}
}
function p2(request, response) {
try {
throw new Error('p2');
} catch (error) {
response.status(500).send(error.toString());
}
}
function p3(request, response) {
throw new Error('p3');
}
function errorHandler(error, request, response, next) { // there must be 4 arguments
response.status(500).send(error.toString());
}

25
examples/express5.js Normal file
View File

@@ -0,0 +1,25 @@
'use strict';
const express = require('express');
const app = express();
app.get('/api/p', p1);
app.get('/api/p', p2);
app.use(errorHandler); // after request handlers
app.listen(3000, () => console.log('Server is running...'));
function p1(request, response, next) {
console.log('p1');
next();
}
function p2(request, response, next) {
console.log('p2');
next('error from p2'); // argument makes it an error
}
function errorHandler(error, request, response, next) { // there must be 4 arguments
response.status(500).send(error.toString());
}

35
examples/express6.js Normal file
View File

@@ -0,0 +1,35 @@
'use strict';
const express = require('express');
const app = express();
app.get('/api/p1', p1);
app.get('/api/p2', p2);
app.use(errorHandler); // after request handlers
app.listen(3000, () => console.log('Server is running...'));
function p1(request, response, next) {
getData()
.then(data => response.end('ok: ' + data))
.catch(error => next(error));
}
function p2(request, response, next) {
getDataFails()
.then(data => response.end(data))
.catch(next);
}
function errorHandler(error, request, response, next) { // there must be 4 arguments
response.status(500).send('error: ' + error.toString());
}
function getData() {
return Promise.resolve('some data');
}
function getDataFails() {
return Promise.reject('some error');
}