Skip to content

Commit c49a1c1

Browse files
anonrigGregBrimble
andauthored
add nodejs-http-server-template (#774)
Co-authored-by: Greg Brimble <gbrimble@cloudflare.com>
1 parent fb15f22 commit c49a1c1

File tree

8 files changed

+8664
-70
lines changed

8 files changed

+8664
-70
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Node.js HTTP Server Template for Cloudflare Workers
2+
3+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/nodejs-http-server-template)
4+
5+
<!-- dash-content-start -->
6+
7+
A simple Node.js HTTP server template using the built-in `node:http` module, designed to run on Cloudflare Workers.
8+
9+
<!-- dash-content-end -->
10+
11+
## Quick Start
12+
13+
1. **Install dependencies:**
14+
15+
```bash
16+
npm install
17+
```
18+
19+
2. **Run locally:**
20+
21+
```bash
22+
npm run dev
23+
```
24+
25+
3. **Deploy to Cloudflare Workers:**
26+
```bash
27+
npx wrangler deploy
28+
```
29+
30+
## Usage
31+
32+
The template creates a basic HTTP server:
33+
34+
```javascript
35+
import { createServer } from "node:http";
36+
import { httpServerHandler } from "cloudflare:node";
37+
38+
const server = createServer((req, res) => {
39+
res.writeHead(200, { "Content-Type": "text/plain" });
40+
res.end("Hello from Node.js HTTP server!");
41+
});
42+
43+
server.listen(8080);
44+
export default httpServerHandler({ port: 8080 });
45+
```
46+
47+
## Configuration
48+
49+
The `wrangler.toml` includes the necessary compatibility flags:
50+
51+
```toml
52+
compatibility_flags = ["nodejs_compat"]
53+
compatibility_date = "2025-09-03"
54+
```
55+
56+
## Scripts
57+
58+
- `npm start` - Start the server
59+
- `npm run dev` - Start with hot reload
60+
- `npm test` - Run tests
61+
62+
## Learn More
63+
64+
- [Cloudflare Workers Node.js HTTP Documentation](https://developers.cloudflare.com/workers/runtime-apis/nodejs/http/)
65+
- [Node.js HTTP Module](https://nodejs.org/api/http.html)
66+
67+
## License
68+
69+
MIT

0 commit comments

Comments
 (0)