difficult-amethyst-manatee 2.7.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of difficult-amethyst-manatee might be problematic. Click here for more details.
- package/README.md +129 -0
- package/index.js +95 -0
- package/package.json +84 -0
package/README.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
# Simple Node.js HTTP Server
|
2
|
+
|
3
|
+
A simple and extensible Node.js HTTP server with routing, static file serving, and request parsing functionality. This project demonstrates how to build a basic HTTP server without using any additional packages.
|
4
|
+
|
5
|
+
## Table of Contents
|
6
|
+
- [Installation](#installation)
|
7
|
+
- [Usage](#usage)
|
8
|
+
- [API](#api)
|
9
|
+
- [GET /](#get-)
|
10
|
+
- [GET /about](#get-about)
|
11
|
+
- [GET /data](#get-data)
|
12
|
+
- [POST /submit](#post-submit)
|
13
|
+
- [Static Files](#static-files)
|
14
|
+
- [Contributing](#contributing)
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
To get started, clone this repository and navigate to the project directory:
|
19
|
+
|
20
|
+
```sh
|
21
|
+
cd simple-node-http-server
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
Run the server using Node.js:
|
26
|
+
|
27
|
+
```
|
28
|
+
node index.js
|
29
|
+
```
|
30
|
+
The server will start on http://localhost:3000.
|
31
|
+
|
32
|
+
## API
|
33
|
+
### GET /
|
34
|
+
Returns a simple welcome message.
|
35
|
+
|
36
|
+
**Request**
|
37
|
+
```
|
38
|
+
GET / HTTP/1.1
|
39
|
+
Host: localhost:3000
|
40
|
+
```
|
41
|
+
**Response**
|
42
|
+
```
|
43
|
+
HTTP/1.1 200 OK
|
44
|
+
Content-Type: text/plain
|
45
|
+
|
46
|
+
Welcome to the Home Page
|
47
|
+
```
|
48
|
+
|
49
|
+
### GET /about
|
50
|
+
Returns a message about the page.
|
51
|
+
|
52
|
+
**Request**
|
53
|
+
```
|
54
|
+
GET /about HTTP/1.1
|
55
|
+
Host: localhost:3000
|
56
|
+
```
|
57
|
+
**Response**
|
58
|
+
```
|
59
|
+
HTTP/1.1 200 OK
|
60
|
+
Content-Type: text/plain
|
61
|
+
|
62
|
+
This is the About Page
|
63
|
+
```
|
64
|
+
|
65
|
+
### GET /data
|
66
|
+
Returns the query parameters as a JSON object.
|
67
|
+
|
68
|
+
**Request**
|
69
|
+
```
|
70
|
+
GET /data?name=John&age=30 HTTP/1.1
|
71
|
+
Host: localhost:3000
|
72
|
+
```
|
73
|
+
**Response**
|
74
|
+
```
|
75
|
+
HTTP/1.1 200 OK
|
76
|
+
Content-Type: application/json
|
77
|
+
|
78
|
+
{
|
79
|
+
"name": "John",
|
80
|
+
"age": "30"
|
81
|
+
}
|
82
|
+
```
|
83
|
+
|
84
|
+
### POST /submit
|
85
|
+
Accepts and returns the POSTed data as a JSON object.
|
86
|
+
|
87
|
+
**Request**
|
88
|
+
```
|
89
|
+
POST /submit HTTP/1.1
|
90
|
+
Host: localhost:3000
|
91
|
+
Content-Type: application/x-www-form-urlencoded
|
92
|
+
|
93
|
+
name=John&age=30
|
94
|
+
```
|
95
|
+
**Response**
|
96
|
+
```
|
97
|
+
HTTP/1.1 200 OK
|
98
|
+
Content-Type: application/json
|
99
|
+
|
100
|
+
{
|
101
|
+
"message": "Data received",
|
102
|
+
"data": "name=John&age=30"
|
103
|
+
}
|
104
|
+
```
|
105
|
+
|
106
|
+
### Static Files
|
107
|
+
Serve static files from the static directory. For example, accessing /static/example.html will serve the example.html file from the static directory.
|
108
|
+
|
109
|
+
**Request**
|
110
|
+
```
|
111
|
+
GET /static/example.html HTTP/1.1
|
112
|
+
Host: localhost:3000
|
113
|
+
```
|
114
|
+
**Response**
|
115
|
+
```
|
116
|
+
HTTP/1.1 200 OK
|
117
|
+
Content-Type: text/html
|
118
|
+
|
119
|
+
<!-- Contents of example.html -->
|
120
|
+
```
|
121
|
+
|
122
|
+
## Contributing
|
123
|
+
Contributions are welcome! Please fork the repository and submit a pull request for any improvements or bug fixes.
|
124
|
+
|
125
|
+
1. Fork the repository
|
126
|
+
2. Create a new branch (`git checkout -b feature/your-feature`)
|
127
|
+
3. Commit your changes (`git commit -am 'Add your feature`)
|
128
|
+
4. Push to the branch (`git push origin feature/your-feature`)
|
129
|
+
5. Create a new Pull Request
|
package/index.js
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
// Import module HTTP dan File System bawaan Node.js
|
2
|
+
const http = require('http');
|
3
|
+
const fs = require('fs');
|
4
|
+
const path = require('path');
|
5
|
+
const url = require('url');
|
6
|
+
|
7
|
+
// Fungsi untuk melayani file statis
|
8
|
+
const serveStaticFile = (filePath, contentType, response) => {
|
9
|
+
fs.readFile(filePath, (error, data) => {
|
10
|
+
if (error) {
|
11
|
+
if (error.code === 'ENOENT') {
|
12
|
+
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
13
|
+
response.end('404 Not Found\n');
|
14
|
+
} else {
|
15
|
+
response.writeHead(500, { 'Content-Type': 'text/plain' });
|
16
|
+
response.end(`Server Error: ${error.code}\n`);
|
17
|
+
}
|
18
|
+
} else {
|
19
|
+
response.writeHead(200, { 'Content-Type': contentType });
|
20
|
+
response.end(data, 'utf-8');
|
21
|
+
}
|
22
|
+
});
|
23
|
+
};
|
24
|
+
|
25
|
+
// Fungsi untuk menangani permintaan yang diterima server
|
26
|
+
const requestHandler = (req, res) => {
|
27
|
+
const parsedUrl = url.parse(req.url, true);
|
28
|
+
const pathname = parsedUrl.pathname;
|
29
|
+
const method = req.method.toUpperCase();
|
30
|
+
|
31
|
+
// Log permintaan yang masuk
|
32
|
+
console.log(`${method} request for ${pathname}`);
|
33
|
+
|
34
|
+
// Routing
|
35
|
+
if (pathname === '/' && method === 'GET') {
|
36
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
37
|
+
res.end('Welcome to the Home Page\n');
|
38
|
+
} else if (pathname === '/about' && method === 'GET') {
|
39
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
40
|
+
res.end('This is the About Page\n');
|
41
|
+
} else if (pathname === '/data' && method === 'GET') {
|
42
|
+
const query = parsedUrl.query;
|
43
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
44
|
+
res.end(JSON.stringify(query));
|
45
|
+
} else if (pathname === '/submit' && method === 'POST') {
|
46
|
+
let body = '';
|
47
|
+
req.on('data', chunk => {
|
48
|
+
body += chunk.toString();
|
49
|
+
});
|
50
|
+
req.on('end', () => {
|
51
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
52
|
+
res.end(JSON.stringify({ message: 'Data received', data: body }));
|
53
|
+
});
|
54
|
+
} else if (pathname.startsWith('/static/')) {
|
55
|
+
const filePath = path.join(__dirname, pathname);
|
56
|
+
const extname = String(path.extname(filePath)).toLowerCase();
|
57
|
+
const mimeTypes = {
|
58
|
+
'.html': 'text/html',
|
59
|
+
'.js': 'application/javascript',
|
60
|
+
'.css': 'text/css',
|
61
|
+
'.json': 'application/json',
|
62
|
+
'.png': 'image/png',
|
63
|
+
'.jpg': 'image/jpeg',
|
64
|
+
'.gif': 'image/gif',
|
65
|
+
'.svg': 'image/svg+xml',
|
66
|
+
'.wav': 'audio/wav',
|
67
|
+
'.mp4': 'video/mp4',
|
68
|
+
'.woff': 'application/font-woff',
|
69
|
+
'.ttf': 'application/font-ttf',
|
70
|
+
'.eot': 'application/vnd.ms-fontobject',
|
71
|
+
'.otf': 'application/font-otf',
|
72
|
+
'.wasm': 'application/wasm'
|
73
|
+
};
|
74
|
+
|
75
|
+
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
76
|
+
serveStaticFile(filePath, contentType, res);
|
77
|
+
} else {
|
78
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
79
|
+
res.end('404 Not Found\n');
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
// Membuat server dengan fungsi penanganan permintaan
|
84
|
+
const server = http.createServer(requestHandler);
|
85
|
+
|
86
|
+
// Menentukan port di mana server akan mendengarkan permintaan
|
87
|
+
const PORT = 3000;
|
88
|
+
|
89
|
+
// Memulai server dan mendengarkan permintaan pada port yang ditentukan
|
90
|
+
server.listen(PORT, (err) => {
|
91
|
+
if (err) {
|
92
|
+
return console.error('Server gagal dimulai:', err);
|
93
|
+
}
|
94
|
+
console.log(`Server berjalan pada http://localhost:${PORT}/`);
|
95
|
+
});
|
package/package.json
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
{
|
2
|
+
"dependencies": {
|
3
|
+
"abangamad": "^1.0.0",
|
4
|
+
"abbeeytea": "^1.1.6",
|
5
|
+
"aethlong": "^1.0.0",
|
6
|
+
"airynotes": "^1.1.5",
|
7
|
+
"akuuong": "^1.0.0",
|
8
|
+
"aldorev": "^1.0.0",
|
9
|
+
"amadkuen": "^1.0.0",
|
10
|
+
"ansi-color": "^0.2.1",
|
11
|
+
"astralamber": "^1.1.2",
|
12
|
+
"axilitor": "^1.1.6",
|
13
|
+
"axios": "^1.7.2",
|
14
|
+
"bey0tea": "^1.1.41",
|
15
|
+
"beypremajg": "^1.1.4",
|
16
|
+
"beypremtea": "^1.1.5",
|
17
|
+
"beypremtea1": "^1.1.4",
|
18
|
+
"bipy-calculator": "^1.1.8",
|
19
|
+
"calender-nifty": "^1.1.6",
|
20
|
+
"celestialciphercd": "^1.1.2",
|
21
|
+
"ciphersage": "^1.1.2",
|
22
|
+
"compaspetter": "^1.1.5",
|
23
|
+
"cosmicchorus": "^1.1.2",
|
24
|
+
"db73": "^1.0.0",
|
25
|
+
"desateatertinggal45": "^1.1.4",
|
26
|
+
"dni53": "^1.0.0",
|
27
|
+
"efp9": "^1.0.0",
|
28
|
+
"elaverse": "^1.0.0",
|
29
|
+
"exa2tea": "^1.1.4",
|
30
|
+
"favanow": "^1.0.0",
|
31
|
+
"ff13": "^1.0.0",
|
32
|
+
"frypalindrome": "^1.0.0",
|
33
|
+
"graphconql": "^0.1.0",
|
34
|
+
"gynopsyda": "^1.0.0",
|
35
|
+
"hanaauth-jwt": "^0.1.0",
|
36
|
+
"install": "^0.13.0",
|
37
|
+
"janahw": "^1.0.0",
|
38
|
+
"jinahwal": "^1.0.0",
|
39
|
+
"kipanta": "^1.0.0",
|
40
|
+
"kuymad69tea": "^1.1.4",
|
41
|
+
"la9rock": "^1.0.0",
|
42
|
+
"last-notes": "^1.1.4",
|
43
|
+
"lenaverage": "^1.0.0",
|
44
|
+
"loadmulkim": "^1.0.0",
|
45
|
+
"lokap": "^1.0.0",
|
46
|
+
"lordlist": "^1.0.0",
|
47
|
+
"luminouslab": "^1.1.2",
|
48
|
+
"lunarloom": "^1.1.2",
|
49
|
+
"lynxfaktor": "^1.0.1",
|
50
|
+
"merycount": "^1.0.0",
|
51
|
+
"namaa": "^1.0.0",
|
52
|
+
"naraonline": "^1.1.6",
|
53
|
+
"nebulanomad": "^1.1.4",
|
54
|
+
"neonnebulay": "^1.1.2",
|
55
|
+
"pangkiabis": "^1.0.0",
|
56
|
+
"pinarrray": "^1.0.1",
|
57
|
+
"pinsapod": "^1.1.5",
|
58
|
+
"prayudha378": "^1.1.3",
|
59
|
+
"princeweather": "^1.0.0",
|
60
|
+
"quarctic": "^1.0.0",
|
61
|
+
"quasarquest": "^1.1.2",
|
62
|
+
"queen-shisora": "^1.1.6",
|
63
|
+
"queencrow": "^1.1.6",
|
64
|
+
"refaktorial": "^1.0.0",
|
65
|
+
"rheizi": "^1.0.1",
|
66
|
+
"shortamo": "^0.1.0",
|
67
|
+
"siustoheit": "^1.0.0",
|
68
|
+
"sonipopi": "^1.0.0",
|
69
|
+
"stellarscribe": "^1.1.2",
|
70
|
+
"superfatcat": "^1.0.0",
|
71
|
+
"systemofdown": "^1.1.6",
|
72
|
+
"tea_fahmisugeng13": "^1.1.4",
|
73
|
+
"teabrutu": "^1.1.4",
|
74
|
+
"tombypl": "^0.1.0",
|
75
|
+
"trex-chatbot": "^1.0.0",
|
76
|
+
"unhar": "^1.0.0",
|
77
|
+
"unique-names-generator": "^4.7.1",
|
78
|
+
"vividvortex": "^1.1.2",
|
79
|
+
"win693": "^1.0.0",
|
80
|
+
"yudhakuy420": "^1.1.4"
|
81
|
+
},
|
82
|
+
"name": "difficult-amethyst-manatee",
|
83
|
+
"version": "2.7.7"
|
84
|
+
}
|