Skip to content

Commit 65c49a3

Browse files
committed
added pact-node-provider example
1 parent 5fb2d69 commit 65c49a3

File tree

9 files changed

+2446
-34
lines changed

9 files changed

+2446
-34
lines changed

pact/pact-node-provider/app.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
1-
var createError = require('http-errors');
2-
var express = require('express');
3-
var path = require('path');
4-
var logger = require('morgan');
5-
6-
var heroesRouter = require('./routes/heroes');
7-
8-
var app = express();
1+
const createError = require('http-errors');
2+
const express = require('express');
3+
const path = require('path');
4+
const logger = require('morgan');
5+
const heroesRouter = require('./routes/heroes');
6+
const providerStateRouter = require('./routes/provider_state');
7+
const app = express();
98

109
app.use(logger('dev'));
1110
app.use(express.json());
12-
app.use(express.urlencoded({ extended: false }));
11+
app.use(express.urlencoded({extended: false}));
1312

1413
app.use('/heroes', heroesRouter);
1514

15+
registerPactEndpoint();
16+
1617
// catch 404 and forward to error handler
17-
app.use(function(req, res, next) {
18-
next(createError(404));
18+
app.use(function (req, res, next) {
19+
next(createError(404));
1920
});
2021

2122
module.exports = app;
23+
24+
/**
25+
* Registers a special endpoint for pact provider tests that allows to
26+
* set the server into a certain "provider state".
27+
*
28+
* This endpoint is only registered if the environment variable "PACT_MODE"
29+
* is set to "true".
30+
*/
31+
function registerPactEndpoint() {
32+
const pactMode = (process.env.PACT_MODE === 'true');
33+
if (pactMode) {
34+
app.use('/provider-state', providerStateRouter);
35+
}
36+
}

0 commit comments

Comments
 (0)