Skip to content

Commit ac10101

Browse files
committed
add example
1 parent f883a6f commit ac10101

File tree

9 files changed

+5044
-0
lines changed

9 files changed

+5044
-0
lines changed

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

example/mock/product.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
'GET /api/product/:id': (req, res) => {
5+
const { id } = req.params;
6+
res.sendFile(path.join(__dirname, `products/${id}.json`));
7+
},
8+
};

example/mock/products/121.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 121,
3+
"name": "banana",
4+
"price": 5.28
5+
}

example/mock/products/122.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 122,
3+
"name": "apple",
4+
"price": 3.58
5+
}

example/mock/user.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
'GET /api/user/info': {
3+
id: 101,
4+
userName: 'bob',
5+
email: 'bob@gmail.com',
6+
firstName: 'Bob',
7+
lastName: 'Bushee',
8+
},
9+
10+
'POST /api/user/login': (req, res) => {
11+
const { userName, password } = req.body;
12+
if (userName === 'bob' && password === 'password') {
13+
res.send({
14+
success: true,
15+
});
16+
} else {
17+
res.send({
18+
success: false,
19+
});
20+
}
21+
},
22+
};

example/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "express-mock-api-example",
3+
"version": "1.0.0",
4+
"description": "Example of using express-mock-api-middleware for mocking restful APIs",
5+
"license": "MIT",
6+
"main": "server.js",
7+
"scripts": {
8+
"start": "node server.js"
9+
},
10+
"dependencies": {
11+
"express": "^4.17.1",
12+
"express-mock-api-middleware": "1.0.5"
13+
}
14+
}

example/server.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const path = require('path');
2+
const express = require('express');
3+
const mockApiMiddleware = require('express-mock-api-middleware');
4+
5+
const app = express();
6+
app.use(
7+
'/',
8+
mockApiMiddleware(path.resolve(__dirname, 'mock'), {
9+
ignore: ['asm.js'],
10+
})
11+
);
12+
13+
const host = 'localhost';
14+
const port = 4000;
15+
16+
app.listen(port, host, (error) => {
17+
if (error) {
18+
console.error(error);
19+
return;
20+
}
21+
console.info(`http://${host}:${port}`);
22+
console.log(`You can access the mock APIs via
23+
http://${host}:${port}/api/user/info
24+
http://${host}:${port}/api/user/login (POST)
25+
http://${host}:${port}/api/product/121
26+
http://${host}:${port}/api/product/122`);
27+
});

0 commit comments

Comments
 (0)