Installations
npm install express-mock-restful
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
12.18.4
NPM Version
6.14.6
Score
68.8
Supply Chain
98.4
Quality
74.3
Maintenance
50
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
xuxihai123
Download Statistics
Total Downloads
15,320
Last Day
1
Last Week
13
Last Month
125
Last Year
1,806
GitHub Statistics
2 Stars
19 Commits
1 Forks
2 Watching
3 Branches
1 Contributors
Bundle Size
481.38 kB
Minified
198.35 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.1.3
Package Id
express-mock-restful@1.1.3
Unpacked Size
25.09 kB
Size
5.71 kB
File Count
22
NPM Version
6.14.6
Node Version
12.18.4
Total Downloads
Cumulative downloads
Total Downloads
15,320
Last day
-92.9%
1
Compared to previous day
Last week
-69.8%
13
Compared to previous week
Last month
-42.9%
125
Compared to previous month
Last year
-43.8%
1,806
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
express-mock-restful
express-mock is a express middleware that creates mocks for REST APIs. It will be helpful when you try to test your application without the actual REST API server.
Features:
🔥 Built in support for hot Mocker file replacement.
🚀 Quickly and easily configure the API via JSON.
Installation
1npm install express-mock-restful --save-dev
Usage
express-mock-restful dev support mock, configured in mocker.js
. refresh mock with require('express-mock-restful').refresh api
1const mockMap = { 2 'GET /api/user': { 3 id: 1, 4 username: 'kenny', 5 sex: 6, 6 }, 7 'GET /api/user/list': [ 8 { 9 id: 1, 10 username: 'kenny', 11 sex: 6, 12 }, 13 { 14 id: 2, 15 username: 'kenny', 16 sex: 6, 17 }, 18 ], 19 'POST /api/login/account': (req, res) => { 20 const { password, username } = req.body; 21 if (password === '888888' && username === 'admin') { 22 return res.json({ 23 status: 'ok', 24 code: 0, 25 token: 'sdfsdfsdfdsf', 26 data: { 27 id: 1, 28 username: 'kenny', 29 sex: 6, 30 }, 31 }); 32 } else { 33 return res.json({ 34 status: 'error', 35 code: 403, 36 }); 37 } 38 }, 39 'DELETE /api/user/:id': (req, res) => { 40 console.log('---->', req.body); 41 console.log('---->', req.params.id); 42 res.send({ status: 'ok', message: '删除成功!' }); 43 }, 44}; 45module.exports = mockMap;
simple usage
1const express = require('express'); 2const path = require('path'); 3const chokidar = require('chokidar'); 4const app = express(); 5+ const apiMocker = require('express-mock-restful'); 6 7+ const mockFile = path.resolve(__dirname, './mocker.js'); 8+ app.use(apiMocker(require(mockFile))); 9// or app.use(apiMocker({ entry: './mocker.js' })); 10 11app.listen(3000); 12console.log('server listen on http://127.0.0.1:3000/');
Using with refresh mock setting
1const express = require('express'); 2const path = require('path'); 3const chokidar = require('chokidar'); 4const app = express(); 5+ const apiMocker = require('express-mock-restful'); 6 7+ const mockFile = path.resolve(__dirname, './mocker.js'); 8+ app.use(apiMocker(require(mockFile))); 9 10// watch file change refresh mock setting 11+ chokidar.watch(mockFile).on('all', (event, path) => { 12+ try { 13+ delete require.cache[require.resolve(mockFile)]; 14+ apiMocker.refresh(require(mockFile)); 15+ console.log('refresh...'); 16+ } catch (err) { 17+ console.log(err); 18+ } 19+ }); 20 21app.listen(3000); 22console.log('server listen on http://127.0.0.1:3000/');
Using with Webpack
To use api mocker on your Webpack projects, simply add a setup options to your webpack-dev-server options:
Change your config file to tell the dev server where to look for files: webpack.config.js
.
1const path = require('path'); 2const HtmlWebpackPlugin = require('html-webpack-plugin'); 3+ const apiMocker = require('express-mock-restful'); 4 5module.exports = { 6 mocker: { 7 app: './src/index.js', 8 print: './src/print.js' 9 }, 10 devtool: 'inline-source-map', 11+ devServer: { 12+ ... 13+ before(app){ 14+ app.use(apiMocker(require('./mocker/index.js')); 15+ } 16+ }, 17 plugins: [ 18 new HtmlWebpackPlugin({ 19 title: 'Development' 20 }) 21 ], 22 output: { 23 filename: '[name].bundle.js', 24 path: path.resolve(__dirname, 'dist') 25 } 26};
Must have a file suffix! For example: ./mocker.js
.
Let's add a script to easily run the dev server as well: package.json
1 { 2 "name": "development", 3 "version": "1.0.0", 4 "description": "", 5 "main": "webpack.config.js", 6 "scripts": { 7 "test": "echo \"Error: no test specified\" && exit 1", 8+ "start": "webpack-dev-server --open", 9 "build": "webpack" 10 }, 11 "keywords": [], 12 "author": "", 13 "license": "MIT", 14 "devDependencies": { 15 .... 16 } 17 }
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/haixuxu/express-mock-restful/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/haixuxu/express-mock-restful/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:27: update your workflow using https://app.stepsecurity.io/secureworkflow/haixuxu/express-mock-restful/test.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
3.1
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreGathering detailed insights and metrics for express-mock-restful