Skip to content

Commit 87b081c

Browse files
Merge pull request #2 from opensource254/dev-stan
Mock API Proposal
2 parents a0ff871 + 922bb3e commit 87b081c

File tree

19 files changed

+1945
-1384
lines changed

19 files changed

+1945
-1384
lines changed

.gitignore

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

README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
# TestAPI
2-
ZachyAPI is a free Fake Online REST API for Testing and Prototyping.
3-
You can make requests to the endpoints:
4-
/users
5-
/companies
6-
/posts
7-
The following are sample requests:
8-
https://usersapi.p.rapidapi.com/users # for users endpoint
9-
https://usersapi.p.rapidapi.com/companies #for companies endpoint
10-
https://usersapi.p.rapidapi.com/posts #for posts endpoint
11-
12-
# I am an Open Source Champion.
13-
14-
1+
# mock-api
2+
TestAPI is a fake online REST API for testing and prototyping.

app.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const express = require('express');
2+
const path = require('path');
3+
const cookieParser = require('cookie-parser');
4+
const logger = require('morgan');
5+
const cors = require('cors')
6+
7+
const indexRouter = require('./routes/index');
8+
const usersRouter = require('./routes/users');
9+
const postsRouter = require('./routes/posts')
10+
const productRouter = require('./routes/products')
11+
12+
const app = express();
13+
14+
app.use(logger('dev'));
15+
app.use(express.json());
16+
app.use(express.urlencoded({ extended: false }));
17+
app.use(cookieParser());
18+
app.use(express.static(path.join(__dirname, 'public')));
19+
app.use(cors())
20+
21+
app.use('/', indexRouter);
22+
app.use('/users', usersRouter);
23+
app.use('/posts', postsRouter);
24+
app.use('/products', productRouter)
25+
26+
module.exports = app;

bin/www

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const app = require('../app');
8+
const debug = require('debug')('mock-api:server');
9+
const http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
const port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
const server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
const port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
const bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
const addr = server.address();
86+
const bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

0 commit comments

Comments
 (0)