2019-01-15 11:09:55 -05:00
|
|
|
# http
|
2018-12-18 23:30:44 -05:00
|
|
|
|
2019-01-15 11:09:55 -05:00
|
|
|
A framework for creating HTTP/HTTPS server.
|
|
|
|
|
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
|
|
|
```
|
|
|
|
|
|
|
|
To set a `Cookie` you can add `CookieOptions` to properly set your `Cookie`
|
|
|
|
|
|
|
|
```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
|
|
|
```
|
|
|
|
|
|
|
|
Deleting a `Cookie` will set its expiration date before now.
|
|
|
|
Forcing the browser to delete it.
|
|
|
|
|
|
|
|
```ts
|
2019-05-23 12:05:00 -04:00
|
|
|
import { Response } from "https://deno.land/std/http/server.ts";
|
2019-04-27 19:07:11 -04:00
|
|
|
import { delCookie } from "https://deno.land/std/http/cookie.ts";
|
|
|
|
|
2019-05-23 12:05:00 -04:00
|
|
|
let response: Response = {};
|
|
|
|
delCookie(response, "deno");
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2019-01-15 11:09:55 -05:00
|
|
|
## Example
|
2018-12-18 23:30:44 -05:00
|
|
|
|
|
|
|
```typescript
|
2019-03-06 10:24:53 -05:00
|
|
|
import { serve } from "https://deno.land/std/http/server.ts";
|
2019-02-19 12:38:19 -05:00
|
|
|
const s = serve("0.0.0.0:8000");
|
2018-12-18 23:30:44 -05:00
|
|
|
|
|
|
|
async function main() {
|
2019-02-19 12:38:19 -05:00
|
|
|
for await (const req of s) {
|
|
|
|
req.respond({ body: new TextEncoder().encode("Hello World\n") });
|
|
|
|
}
|
2018-12-18 23:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|
|
|
|
```
|
|
|
|
|
|
|
|
### File Server
|
|
|
|
|
|
|
|
A small program for serving local files over HTTP.
|
|
|
|
|
2019-06-29 10:30:01 -04:00
|
|
|
Install it by using `deno install`
|
2018-12-18 23:30:44 -05:00
|
|
|
|
2019-05-31 08:39:05 -04:00
|
|
|
```sh
|
2019-06-29 10:30:01 -04:00
|
|
|
deno install file_server https://deno.land/std/http/file_server.ts --allow-net --allow-read
|
2018-12-18 23:30:44 -05:00
|
|
|
```
|