Skip to content

Commit

Permalink
Add server files for Connect examples
Browse files Browse the repository at this point in the history
One of the reviewers found the examples were not easy to run without
suitable server files, so I've added them to the first four samples
  • Loading branch information
alexyoung committed Nov 21, 2016
1 parent daa2daf commit 112bff9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ch07-connect-and-express/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ch07-connect-and-express",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
2 changes: 1 addition & 1 deletion ch07-connect-and-express/listing7_1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "listing7_1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const connect = require('connect');

function logger(req, res, next) {
console.log('%s %s', req.method, req.url);
next();
}

function hello(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('hello world');
}

connect()
.use(logger)
.use(hello)
.listen(3000);

.listen(3000);
2 changes: 1 addition & 1 deletion ch07-connect-and-express/listing7_2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "listing7_2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
12 changes: 12 additions & 0 deletions ch07-connect-and-express/listing7_3/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const connect = require('connect');
const setup = require('./logger.js')

function hello(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('hello world');
}

const app = connect()
.use(setup(':method :url'))
.use(hello)
.listen(3000);
26 changes: 12 additions & 14 deletions ch07-connect-and-express/listing7_4/errors.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
var env = process.env.NODE_ENV || 'development';
const env = process.env.NODE_ENV || 'development';

function errorHandler() {
return function(err, req, res, next) {
res.statusCode = 500;
switch (env) {
case 'development':
console.error('Error:');
console.error(err);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(err));
break;
default:
res.end('Server error');
}
function errorHandler(err, req, res, next) {
res.statusCode = 500;
switch (env) {
case 'development':
console.error('Error caught by errorHandler:');
console.error(err);
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(err));
break;
default:
res.end('Server error');
}
}

Expand Down
14 changes: 14 additions & 0 deletions ch07-connect-and-express/listing7_4/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function setup(format) {
const regexp = /:(\w+)/g;

return function createLogger(req, res, next) {
const str = format.replace(regexp, (match, property) => {
return req[property];
});

console.log(str);
next();
}
}

module.exports = setup;
2 changes: 1 addition & 1 deletion ch07-connect-and-express/listing7_4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "listing7_4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
14 changes: 14 additions & 0 deletions ch07-connect-and-express/listing7_4/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const connect = require('connect');
const setup = require('./logger.js');
const errorHandler = require('./errors.js');

function hello(req, res, next) {
res.setHeader('Content-Type', 'text/plain');
next(new Error('Intentional error'));
}

const app = connect()
.use(setup(':method :url'))
.use(hello)
.use(errorHandler)
.listen(3000);

0 comments on commit 112bff9

Please sign in to comment.