commit 18c31430f1897074bed9c4753897e4ca8aa96ef8 Author: Märt Kalmo Date: Tue May 9 15:42:06 2017 +0300 done state diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7072bbc --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +node_modules + +/.settings +/.classpath +/.project + +/.idea +*.iml +*.log diff --git a/examples/express1.js b/examples/express1.js new file mode 100644 index 0000000..d48256f --- /dev/null +++ b/examples/express1.js @@ -0,0 +1,8 @@ +'use strict'; + +const express = require('express'); +const app = express(); + +app.use(express.static('./')); + +app.listen(3000); \ No newline at end of file diff --git a/examples/express2.js b/examples/express2.js new file mode 100644 index 0000000..310d92f --- /dev/null +++ b/examples/express2.js @@ -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 })); +} diff --git a/examples/express3.js b/examples/express3.js new file mode 100644 index 0000000..a0b54d3 --- /dev/null +++ b/examples/express3.js @@ -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(); +} diff --git a/examples/express4.js b/examples/express4.js new file mode 100644 index 0000000..efc16f1 --- /dev/null +++ b/examples/express4.js @@ -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()); +} diff --git a/examples/express5.js b/examples/express5.js new file mode 100644 index 0000000..a8f2452 --- /dev/null +++ b/examples/express5.js @@ -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()); +} diff --git a/examples/express6.js b/examples/express6.js new file mode 100644 index 0000000..374bbdf --- /dev/null +++ b/examples/express6.js @@ -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'); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..441dbf5 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "exexpress", + "version": "1.0.0", + "scripts": { + "start": "nodemon server.js" + }, + "devDependencies": { + "body-parser": "^1.17.1", + "express": "^4.15.2", + "nodemon": "^1.11.0" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..d39881f --- /dev/null +++ b/server.js @@ -0,0 +1,50 @@ +'use strict'; + +const express = require('express'); +const bodyParser = require('body-parser'); + +const Task = require('./task'); +const TaskService = require('./task-service'); +const dao = new TaskService(); +const app = express(); + +app.use(bodyParser.json()); // before request handlers + +app.get('/api/tasks', getTasks); +app.get('/api/tasks/:id', getTask); +app.post('/api/tasks', saveTask); +app.delete('/api/tasks/:id', deleteTask); + +app.use(errorHandler); // after request handlers + +app.listen(3000, () => console.log('Server is running...')); + +function getTasks(request, response, next) { + dao.getTasks() + .then(tasks => response.json(tasks)) + .catch(next); +} + +function getTask(request, response, next) { + var id = request.params.id; + dao.getTask(id) + .then(task => response.json(task)) + .catch(next); +} + +function saveTask(request, response, next) { + dao.saveTask(request.body) + .then(() => response.end()) + .catch(next); +} + +function deleteTask(request, response, next) { + var id = request.params.id; + dao.deleteTask(id) + .then(() => response.end()) + .catch(next); +} + +function errorHandler(error, request, response, next) { // there must be 4 arguments + response.status(500).json({ error: error.toString() }); +} diff --git a/task-service.js b/task-service.js new file mode 100644 index 0000000..9a08058 --- /dev/null +++ b/task-service.js @@ -0,0 +1,56 @@ +'use strict'; + +const Task = require('./task'); + +class MemTaskService { + + constructor() { + this.tasks = [ + Task.withId('1', 'Task 1'), + Task.withId('2', 'Task 2') + ]; + } + + clone(what) { + return JSON.parse(JSON.stringify(what)); + } + + getTasks() { + return Promise.resolve(this.clone(this.tasks)); + } + + getTask(id) { + let found = this.clone(this.tasks) + .filter(each => each._id === id) + .pop(); + + return found ? Promise.resolve(found) : Promise.reject('no task with id: ' + id); + } + + saveTask(task) { + if (!task._id) { + task._id = this.getId(); + this.tasks.push(task); + return Promise.resolve(); + } + + this.tasks = this.tasks + .map(each =>each._id === task._id ? task : each); + + return Promise.resolve(); + } + + deleteTask(id) { + this.tasks = this.tasks + .filter(each => each._id !== id); + + return Promise.resolve(); + } + + getId() { + return (Math.random() + 'A').substr(2); + } + +} + +module.exports = MemTaskService; diff --git a/task.js b/task.js new file mode 100644 index 0000000..a8e368d --- /dev/null +++ b/task.js @@ -0,0 +1,15 @@ +'use strict'; + +class Task { + constructor(title) { + this.title = title; + } + + static withId(id, title) { + let task = new Task(title); + task._id = id; + return task; + } +} + +module.exports = Task; \ No newline at end of file