forked from alexyoung/nodejsinaction
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server files for Connect examples
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
Showing
11 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
ch07-connect-and-express/listing7_1/index.js → ...-connect-and-express/listing7_1/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |