done state
This commit is contained in:
commit
18c31430f1
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
node_modules
|
||||
|
||||
/.settings
|
||||
/.classpath
|
||||
/.project
|
||||
|
||||
/.idea
|
||||
*.iml
|
||||
*.log
|
8
examples/express1.js
Normal file
8
examples/express1.js
Normal 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
22
examples/express2.js
Normal 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
21
examples/express3.js
Normal 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
37
examples/express4.js
Normal 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
25
examples/express5.js
Normal 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
35
examples/express6.js
Normal 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');
|
||||
}
|
12
package.json
Normal file
12
package.json
Normal file
@ -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"
|
||||
}
|
||||
}
|
50
server.js
Normal file
50
server.js
Normal file
@ -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() });
|
||||
}
|
56
task-service.js
Normal file
56
task-service.js
Normal file
@ -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;
|
Loading…
Reference in New Issue
Block a user