https://github.com/samuelnovaes/easy-cgi
Easily create a CGI server
https://github.com/samuelnovaes/easy-cgi
cgi easy http server web
Last synced: 11 months ago
JSON representation
Easily create a CGI server
- Host: GitHub
- URL: https://github.com/samuelnovaes/easy-cgi
- Owner: samuelnovaes
- License: bsd-3-clause
- Created: 2017-12-10T20:56:20.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-10T23:05:20.000Z (about 8 years ago)
- Last Synced: 2025-02-15T06:47:57.646Z (12 months ago)
- Topics: cgi, easy, http, server, web
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Easily create a CGI server
[](https://nodei.co/npm/easy-cgi/)
# Installation
- Install [Node.js](https://nodejs.org)
- Install easy-cgi
```
npm install -g easy-cgi
```
# Example (Using Bash)
Create a directory for your app whith your CGI executable files.
App directory
```
app
└---hello.sh
```
app/hello.sh
```bash
#!/bin/sh
# Headers are written first. The special "Status"
# header indicates the response status code
echo "Status: 200"
echo "Content-Type: text/plain"
echo
# Followed by a response body
echo "Hello World!"
```
Give execute permissions to the file
```bash
chmod +x hello.sh
```
Then start your CGI server
```bash
cgi
```
Access the address http://localhost:8080/hello in the browser
# Routing
The routes are created automatically by file name
Example:
- For the `app/hello.sh` file, the route is `/hello`
- For the `app/foo/bar.sh` file, the route is `/foo/bar`
- For the `app/index.sh` file, the route is `/`
- For the `app/foo/index.sh` file, the route is `/foo`
> If you create a `app/src` directory, it will be ignored
### Dynamic routes
Dynamic routes are created adding underscore in file name or directory
Example:
- For the `app/users/_id.sh` file, the route is `/users/`
- For the `app/_foo/bar.sh` file, the route is `//bar`
# Get request values
You can access the request values through the REQUEST environment variable in JSON format
The REQUEST environment variable looks like
```json
{
"body": {},
"params": {},
"query": {},
"headers": {},
"files": {}
}
```
> No sessions are supported yet
# Static files
To serve static files, create a `static` directory