Skip to content

Commit b159807

Browse files
committed
[UPDATE] Markup
1 parent bd1cae5 commit b159807

16 files changed

+199
-64
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# JSON Server API
2+
3+
A lightweight Node.js application that serves an API from a file-based JSON structure. It dynamically generates API routes based on the folder structure, making it easy to create and manage mock APIs without a database.
4+
5+
## Features
6+
* Serves JSON data as an API
7+
* Folder-based dynamic routing
8+
* Built-in caching for improved performance
9+
* Middleware for logging and security headers
10+
* Supports static file serving and EJS templating
11+
12+
## Installation
13+
14+
```sh
15+
git clone https://github.com/yourusername/json-serve-api.git
16+
cd json-serve-api
17+
npm install
18+
```
19+
20+
### Usage
21+
22+
```sh
23+
npm start
24+
```
25+
26+
By default, the server runs on `http://localhost:3001`.
27+
28+
### API Structure
29+
30+
Place your JSON files inside the api folder. The folder structure determines the API routes.
31+
32+
For example, given the following structure:
33+
34+
```sh
35+
api/
36+
users/
37+
list.json
38+
details.jsonYou can access:
39+
```
40+
41+
You can access:
42+
43+
- GET /api/users/list → Returns list.json
44+
- GET /api/users/details → Returns details.json
45+
46+
### Middleware
47+
48+
The application includes:
49+
50+
* Logging: Logs incoming requests
51+
* Security Headers: Adds basic security headers
52+
* Compression: Enables Gzip compression
53+
* Static File Serving: Serves files from the public folder
54+
## Configuration
55+
Set PORT in the environment variables to change the server port.
56+
## License
57+
This project is licensed under the MIT License.

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ function handleHeaders(req, res, next) {
2828
next();
2929
}
3030

31-
app.use(cache('5 minutes'));
31+
// app.use(cache('5 minutes'));
3232
app.use(express.static(publicDir));
3333
app.use(express.json());
3434
app.use(compression())
3535
app.use(handleLog);
3636
app.use(handleHeaders);
37+
app.set('view engine', 'ejs');
3738
app.use('/', homeRoute);
3839
app.use('/api/', apiRoute);
3940
app.use('*', notFoundRoute);

module/fs.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ function getFiles(targetDir = apiBaseDir) {
3030
if (entry.isDirectory()) {
3131
explore(entryPath);
3232
} else if (entry.isFile()) {
33-
const getParentDir = entry.path.replace(path.resolve('api'), '');
33+
const parentDir = entry.path
34+
.replace(path.resolve('api'), '')
35+
.replace(/\\/g, '/');
36+
const name = entry.name.split('.')[0];
37+
3438
fileList.push({
35-
name: entry.name.split('.')[0],
3639
path: entryPath,
37-
parentPath: getParentDir
40+
url: parentDir + '/' + name,
3841
});
3942
}
4043
}

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"private": false,
3-
"name": "json-serve-api",
3+
"name": "json-server-api",
44
"version": "0.1.0",
55
"license": "MIT",
66
"bugs": {
7-
"url": "https://github.com/arashyeganeh/json-serve-api/issues"
7+
"url": "https://github.com/arashyeganeh/json-server-api/issues"
88
},
99
"author": {
1010
"name": "Arash Yeganeh",
1111
"url": "https://github.com/arashyeganeh"
1212
},
1313
"repository": {
1414
"type": "git",
15-
"url": "https://github.com/arashyeganeh/json-serve-api.git"
15+
"url": "https://github.com/arashyeganeh/json-server-api.git"
1616
},
1717
"scripts": {
1818
"dev": "nodemon app.js",
@@ -35,5 +35,8 @@
3535
"#module/*": "./module/*",
3636
"#route/*": "./route/*",
3737
"#app.js": "./app.js"
38+
},
39+
"nodemonConfig": {
40+
"ext": "js,json,ejs,html,css"
3841
}
3942
}

public/apple-touch-icon.png

20.4 KB
Loading

public/favicon-96x96.png

7.7 KB
Loading

public/favicon.ico

14.7 KB
Binary file not shown.

public/favicon.svg

Lines changed: 1 addition & 0 deletions
Loading

public/index.html

Lines changed: 0 additions & 44 deletions
This file was deleted.

public/site.webmanifest

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "JSON Server API",
3+
"short_name": "",
4+
"icons": [
5+
{
6+
"src": "/web-app-manifest-192x192.png",
7+
"sizes": "192x192",
8+
"type": "image/png",
9+
"purpose": "maskable"
10+
},
11+
{
12+
"src": "/web-app-manifest-512x512.png",
13+
"sizes": "512x512",
14+
"type": "image/png",
15+
"purpose": "maskable"
16+
}
17+
],
18+
"theme_color": "#ffffff",
19+
"background_color": "#ffffff",
20+
"display": "standalone"
21+
}

0 commit comments

Comments
 (0)