starting state

This commit is contained in:
Märt Kalmo 2017-04-04 15:57:10 +03:00
parent affc743063
commit e488ae6eec
7 changed files with 163 additions and 17 deletions

View File

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

View File

@ -0,0 +1,20 @@
'use strict';
const express = require('express');
const app = express();
app.get('/api/contacts', getContacts);
app.get('/api/contacts/:id', getContact);
app.listen(3000);
function getContacts(request, response) {
response.set('Content-Type', 'application/json');
response.end(JSON.stringify([{ id: 1 }, { id: 2 }]));
}
function getContact(request, response) {
var id = request.params.id;
response.set('Content-Type', 'application/json');
response.end(JSON.stringify({ id: id }));
}

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/contacts', getContacts);
app.post('/api/contacts', saveContact);
app.listen(3000);
function getContacts(request, response) {
response.json([{ id: 1 }, { id: 2 }]);
}
function saveContact(request, response) {
console.log(request.body); // request should have correct content-type
response.end();
}

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);
app.listen(3000);
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());
}

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);
app.listen(3000);
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());
}

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);
app.listen(3000);
function p1(request, response, next) {
getData()
.then((data) => response.end('ok: ' + data))
.catch(() => next());
}
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');
}

View File

@ -1,26 +1,26 @@
'use strict';
const mongodb = require('mongodb');
const Dao = require('./dao.js');
const ObjectID = mongodb.ObjectID;
var url = 'mongodb://...';
// insert({ key: 'value1' });
// findAll().then(data => console.log(JSON.stringify(data)));
var data = { name: 'Jill' };
function insert(data) {
mongodb.MongoClient.connect(url).then(db => {
var c = db.collection('test-collection');
c.insertOne(data).then(() => db.close());
});
}
var database;
mongodb.MongoClient.connect(url).then(db => {
database = db;
return db.collection('test-collection').insertOne(data);
}).then(() => {
closeDb(database)
}).catch(error => {
closeDb(database);
throw error;
});
function findAll() {
return mongodb.MongoClient.connect(url).then(db => {
var c = db.collection('test-collection');
return c.find().toArray().then(data => {
db.close();
return data;
});
});
}
function closeDb(database) {
if (database) {
database.close();
}
}