Skip to content

Commit b339bfb

Browse files
committed
feat: clean up CRA stuff and add mock api server
1 parent 990f81b commit b339bfb

File tree

14 files changed

+85
-154
lines changed

14 files changed

+85
-154
lines changed

README.md

+3-67
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,4 @@
1-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
1+
# Mock API React
2+
## Harvey Delaney
23

3-
## Available Scripts
4-
5-
In the project directory, you can run:
6-
7-
### `npm start`
8-
9-
Runs the app in the development mode.<br />
10-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11-
12-
The page will reload if you make edits.<br />
13-
You will also see any lint errors in the console.
14-
15-
### `npm test`
16-
17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19-
20-
### `npm run build`
21-
22-
Builds the app for production to the `build` folder.<br />
23-
It correctly bundles React in production mode and optimizes the build for the best performance.
24-
25-
The build is minified and the filenames include the hashes.<br />
26-
Your app is ready to be deployed!
27-
28-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29-
30-
### `npm run eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `npm run build` fails to minify
67-
68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
4+
This project was created for the article: https://blog.harveydelaney.com/setting-up-a-mock-api-for-your-front-end-react-project/

mock-api/app.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const express = require('express');
2+
const apiMocker = require('connect-api-mocker');
3+
4+
const port = 9000;
5+
const app = express();
6+
7+
app.use('/api', apiMocker('.'));
8+
9+
console.log(`Mock API Server is up and running at: http://localhost:${port}`);
10+
app.listen(port);

mock-api/user/POST.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = (req, res) => {
2+
const userId = req.body.id;
3+
4+
if (userId === 1) {
5+
return res.sendStatus(409);
6+
}
7+
8+
return res.status(201).json({
9+
id: req.body.id,
10+
userName: req.body.userName,
11+
age: req.body.age
12+
});
13+
};

mock-api/user/__userName__/GET.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = (req, res) =>
2+
res.status(200).json({
3+
id: 0,
4+
userName: req.params.userName,
5+
age: 20
6+
});

mock-api/user/harvey/GET.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": 1,
3+
"userName": "harvey",
4+
"age": "24"
5+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (req, res) => res.sendStatus(403);

package-lock.json

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "mock-api-react",
3+
"proxy": "http://localhost:9000/api",
34
"version": "0.1.0",
45
"private": true,
56
"dependencies": {
@@ -30,5 +31,9 @@
3031
"last 1 firefox version",
3132
"last 1 safari version"
3233
]
34+
},
35+
"devDependencies": {
36+
"connect-api-mocker": "^1.7.0",
37+
"express": "^4.17.1"
3338
}
3439
}

src/App.css

-38
This file was deleted.

src/App.js

+32-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,37 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React, { useEffect, useState } from 'react';
2+
3+
const useFetch = (url) => {
4+
const [data, setData] = useState(null);
5+
6+
useEffect(() => {
7+
fetch(url).then(async res => {
8+
if (res.status !== 200) {
9+
setData('uh oh error!');
10+
}
11+
const data = await res.json();
12+
setData(data);
13+
});
14+
}, [setData, url]);
15+
16+
return [data];
17+
}
18+
19+
const App = () => {
20+
const [harveyData] = useFetch('/user/harvey');
21+
const [blahData] = useFetch('/user/blahblahblah');
22+
const [unauthData] = useFetch('/user/unauthorised-user');
423

5-
function App() {
624
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
25+
<div>
26+
<div>
27+
User data: {JSON.stringify(harveyData)}
28+
</div>
29+
<div>
30+
Blah data: {JSON.stringify(blahData)}
31+
</div>
32+
<div>
33+
Unauth data: {JSON.stringify(unauthData)}
34+
</div>
2235
</div>
2336
);
2437
}

src/App.test.js

-9
This file was deleted.

src/index.css

-13
This file was deleted.

src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
3-
import './index.css';
43
import App from './App';
54
import * as serviceWorker from './serviceWorker';
65

src/logo.svg

-7
This file was deleted.

0 commit comments

Comments
 (0)