starting state
This commit is contained in:
parent
affc743063
commit
e488ae6eec
8
express-examples/express1.js
Normal file
8
express-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);
|
20
express-examples/express2.js
Normal file
20
express-examples/express2.js
Normal 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 }));
|
||||
}
|
21
express-examples/express3.js
Normal file
21
express-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/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();
|
||||
}
|
37
express-examples/express4.js
Normal file
37
express-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);
|
||||
|
||||
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());
|
||||
}
|
25
express-examples/express5.js
Normal file
25
express-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);
|
||||
|
||||
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());
|
||||
}
|
35
express-examples/express6.js
Normal file
35
express-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);
|
||||
|
||||
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');
|
||||
}
|
34
mongo.js
34
mongo.js
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user