-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.ts
42 lines (36 loc) · 1.18 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Run this example using:
*
* deno run --allow-net --allow-read ./examples/json/index.ts
*
* if have the repo cloned locally OR
*
* deno run --allow-net --allow-read https://raw.githubusercontent.com/cmorten/opine/main/examples/json/index.ts
*
* if you don't!
*/
import { json, opine } from "../../mod.ts";
const app = opine();
// Use the JSON body parser to set `req.parsedBody` to the
// passed JSON payload. Opine also exposes this value on
// the `req.body` property itself for convenience.
app.use(json());
// Receive `POST` requests to `/` and return the passed
// JSON body.
app.post("/", function (req, res) {
res.send(req.body);
});
if (import.meta.main) {
// You can call listen the same as Express with just
// a port: `app.listen(3000)`, or with any arguments
// that the Deno `http.serve` methods accept. Namely
// an address string, HttpOptions or HttpsOptions
// objects.
app.listen({ port: 3000 });
console.log("Opine started on port 3000");
console.log("Try a `POST /` with any JSON payload!");
console.log(
`curl -X POST http://localhost:3000/ -d '{"message": "Hello Deno!"}' -H "Content-Type: application/json"`,
);
}
export { app };