starting state
This commit is contained in:
parent
18c31430f1
commit
281735374c
41
server.js
41
server.js
@ -1,50 +1,19 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
|
|
||||||
const Task = require('./task');
|
|
||||||
const TaskService = require('./task-service');
|
const TaskService = require('./task-service');
|
||||||
const dao = new TaskService();
|
const dao = new TaskService();
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
app.use(bodyParser.json()); // before request handlers
|
|
||||||
|
|
||||||
app.get('/api/tasks', getTasks);
|
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...'));
|
app.listen(3000, () => console.log('Server is running...'));
|
||||||
|
|
||||||
function getTasks(request, response, next) {
|
function getTasks(request, response) {
|
||||||
dao.getTasks()
|
dao.getTasks()
|
||||||
.then(tasks => response.json(tasks))
|
.then(tasks => {
|
||||||
.catch(next);
|
console.log(tasks);
|
||||||
}
|
response.end('ok');
|
||||||
|
});
|
||||||
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() });
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user