2019-01-15 11:09:55 -05:00
|
|
|
# http
|
2018-12-18 23:30:44 -05:00
|
|
|
|
2019-11-19 01:07:13 -05:00
|
|
|
```typescript
|
|
|
|
import { serve } from "https://deno.land/std/http/server.ts";
|
2020-05-26 10:09:47 -04:00
|
|
|
const server = serve({ port: 8000 });
|
2019-11-19 01:07:13 -05:00
|
|
|
console.log("http://localhost:8000/");
|
2020-05-26 10:09:47 -04:00
|
|
|
for await (const req of server) {
|
2020-01-17 18:44:35 -05:00
|
|
|
req.respond({ body: "Hello World\n" });
|
2019-11-19 01:07:13 -05:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### File Server
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
A small program for serving local files over HTTP.
|
2019-11-19 01:07:13 -05:00
|
|
|
|
|
|
|
```sh
|
2020-05-07 07:48:48 -04:00
|
|
|
deno run --allow-net --allow-read https://deno.land/std/http/file_server.ts
|
2019-11-19 01:07:13 -05:00
|
|
|
> HTTP server listening on http://0.0.0.0:4500/
|
|
|
|
```
|
2019-01-15 11:09:55 -05:00
|
|
|
|
2019-04-27 19:07:11 -04:00
|
|
|
## Cookie
|
|
|
|
|
2019-05-31 08:39:05 -04:00
|
|
|
Helper to manipulate `Cookie` through `ServerRequest` and `Response`.
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
```ts
|
2019-05-23 12:05:00 -04:00
|
|
|
import { ServerRequest } from "https://deno.land/std/http/server.ts";
|
2019-04-27 19:07:11 -04:00
|
|
|
import { getCookies } from "https://deno.land/std/http/cookie.ts";
|
|
|
|
|
2019-05-23 12:05:00 -04:00
|
|
|
let request = new ServerRequest();
|
|
|
|
request.headers = new Headers();
|
|
|
|
request.headers.set("Cookie", "full=of; tasty=chocolate");
|
2019-04-27 19:07:11 -04:00
|
|
|
|
2019-05-23 12:05:00 -04:00
|
|
|
const cookies = getCookies(request);
|
|
|
|
console.log("cookies:", cookies);
|
|
|
|
// cookies: { full: "of", tasty: "chocolate" }
|
2019-04-27 19:07:11 -04:00
|
|
|
```
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
To set a `Cookie` you can add `CookieOptions` to properly set your `Cookie`:
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
```ts
|
2019-05-23 12:05:00 -04:00
|
|
|
import { Response } from "https://deno.land/std/http/server.ts";
|
|
|
|
import { Cookie, setCookie } from "https://deno.land/std/http/cookie.ts";
|
2019-04-27 19:07:11 -04:00
|
|
|
|
2019-05-23 12:05:00 -04:00
|
|
|
let response: Response = {};
|
|
|
|
const cookie: Cookie = { name: "Space", value: "Cat" };
|
|
|
|
setCookie(response, cookie);
|
|
|
|
|
|
|
|
const cookieHeader = response.headers.get("set-cookie");
|
|
|
|
console.log("Set-Cookie:", cookieHeader);
|
|
|
|
// Set-Cookie: Space=Cat
|
2019-04-27 19:07:11 -04:00
|
|
|
```
|
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
Deleting a `Cookie` will set its expiration date before now. Forcing the browser
|
|
|
|
to delete it.
|
2019-04-27 19:07:11 -04:00
|
|
|
|
|
|
|
```ts
|
2019-05-23 12:05:00 -04:00
|
|
|
import { Response } from "https://deno.land/std/http/server.ts";
|
2020-06-25 07:12:09 -04:00
|
|
|
import { deleteCookie } from "https://deno.land/std/http/cookie.ts";
|
2019-04-27 19:07:11 -04:00
|
|
|
|
2019-05-23 12:05:00 -04:00
|
|
|
let response: Response = {};
|
2020-06-25 07:12:09 -04:00
|
|
|
deleteCookie(response, "deno");
|
2019-05-23 12:05:00 -04:00
|
|
|
|
|
|
|
const cookieHeader = response.headers.get("set-cookie");
|
|
|
|
console.log("Set-Cookie:", cookieHeader);
|
|
|
|
// Set-Cookie: deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT
|
2019-04-27 19:07:11 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
**Note**: At the moment multiple `Set-Cookie` in a `Response` is not handled.
|