Skip to content

Commit 460369f

Browse files
committed
[ADD] Production stage
1 parent fef11fb commit 460369f

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,55 @@ A lightweight Node.js application that serves an API from a file-based JSON stru
66
* Serves JSON data as an API
77
* Folder-based dynamic routing
88
* Built-in caching for improved performance
9-
* Middleware for logging and security headers
9+
* Middleware for logging
1010
* Supports static file serving and EJS templating
1111

1212
## Installation
1313

1414
```sh
15-
git clone https://github.com/yourusername/json-serve-api.git
16-
cd json-serve-api
15+
git clone https://github.com/arashyeganeh/json-server-api
16+
cd json-server-api
1717
npm install
1818
```
1919

2020
### Usage
2121

2222
```sh
23-
npm start
23+
npm serve
2424
```
2525

26-
By default, the server runs on `http://localhost:3001`.
26+
By default, the server runs on `http://localhost:3000`.
2727

2828
### API Structure
2929

30-
Place your JSON files inside the api folder. The folder structure determines the API routes.
30+
Place your JSON files inside the `api` folder. The folder structure determines the API routes.
3131

3232
For example, given the following structure:
3333

3434
```sh
3535
api/
36-
users/
37-
list.json
38-
details.jsonYou can access:
36+
posts.json --> http://localhost/api/posts
37+
users.json --> http://localhost/api/users
38+
users/
39+
2.json --> http://localhost/api/users/2
3940
```
4041

4142
You can access:
4243

43-
- GET /api/users/list → Returns list.json
44-
- GET /api/users/details → Returns details.json
44+
- GET /api/posts → Returns posts.json
45+
- GET /api/users → Returns users.json
46+
- GET /api/users/2 → Returns 2.json
4547

4648
### Middleware
4749

4850
The application includes:
4951

5052
* Logging: Logs incoming requests
51-
* Security Headers: Adds basic security headers
5253
* Compression: Enables Gzip compression
5354
* Static File Serving: Serves files from the public folder
5455
## Configuration
55-
Set PORT in the environment variables to change the server port.
56+
By default, the server runs on port `80` and port `3000` in the development stage. To change the port, set the PORT environment variable in `package.json`.
57+
5658
## License
59+
5760
This project is licensed under the MIT License.

app.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const homeRoute = require('#route/home.js');
1111
const apiRoute = require('#route/api.js');
1212
const notFoundRoute = require('#route/notfound.js');
1313

14-
const PORT = parseInt(process.env.PORT, 10) || 3001;
14+
const IS_PRODUCTION = process.env.NODE_ENV === 'production';
15+
const PORT = parseInt(process.env.PORT, 10) || 3000;
16+
1517
const publicDir = path.join(__dirname, 'public');
1618
const app = express();
1719
let cache = apicache.middleware;
@@ -28,7 +30,7 @@ function handleHeaders(req, res, next) {
2830
next();
2931
}
3032

31-
// app.use(cache('5 minutes'));
33+
IS_PRODUCTION && app.use(cache('5 minutes'));
3234
app.use(express.static(publicDir));
3335
app.use(express.json());
3436
app.use(compression())

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"url": "https://github.com/arashyeganeh/json-server-api.git"
1616
},
1717
"scripts": {
18-
"dev": "nodemon app.js",
19-
"serve": "set PORT=80 && node app.js",
18+
"dev": "set PORT=3000 && nodemon app.js",
19+
"serve": "set NODE_ENV=production&& set PORT=80&& node app.js",
2020
"prettier": "prettier . --write"
2121
},
2222
"devDependencies": {

0 commit comments

Comments
 (0)