-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e0ad829
commit e034a51
Showing
12 changed files
with
328 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import { createServer } from "node:http"; | ||
import { createApp, createRouter, defineEventHandler, readBody, toNodeListener } from "h3" | ||
import { | ||
createApp, | ||
createRouter, | ||
defineEventHandler, | ||
readBody, | ||
toNodeListener, | ||
} from "h3"; | ||
|
||
const app = createApp(); | ||
|
||
const router = createRouter() | ||
.get('/', defineEventHandler(() => { | ||
return 'use another HTTP method to enable body parsing' | ||
})) | ||
.post('/', defineEventHandler(async (event) => { | ||
const body = await readBody(event) | ||
.get( | ||
"/", | ||
defineEventHandler(() => { | ||
return "use another HTTP method to enable body parsing"; | ||
}), | ||
) | ||
.post( | ||
"/", | ||
defineEventHandler(async (event) => { | ||
const body = await readBody(event); | ||
|
||
// Use can also use `readFormData` to get a FormData object, `readMultiPartFormData` to get an array of MultiPartData or `readRawBody` to get a Buffer. | ||
// Use can also use `readFormData` to get a FormData object, `readMultiPartFormData` to get an array of MultiPartData or `readRawBody` to get a Buffer. | ||
|
||
return { | ||
body, | ||
} | ||
})) | ||
return { | ||
body, | ||
}; | ||
}), | ||
); | ||
|
||
app.use(router); | ||
|
||
app.use(router) | ||
|
||
createServer(toNodeListener(app)).listen(3000) | ||
createServer(toNodeListener(app)).listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import { createServer } from "node:http"; | ||
import { createApp, createRouter, defineEventHandler, getCookie, setCookie, toNodeListener } from "h3" | ||
import { | ||
createApp, | ||
createRouter, | ||
defineEventHandler, | ||
getCookie, | ||
setCookie, | ||
toNodeListener, | ||
} from "h3"; | ||
|
||
const app = createApp(); | ||
|
||
const router = createRouter() | ||
.get('/', defineEventHandler((event) => { | ||
const sessionId = getCookie(event, 'sessionId') | ||
.get( | ||
"/", | ||
defineEventHandler((event) => { | ||
const sessionId = getCookie(event, "sessionId"); | ||
|
||
return { | ||
sessionId, | ||
} | ||
})) | ||
.post('/login', defineEventHandler((event) => { | ||
setCookie(event, "sessionId", "bar", { httpOnly: true }) // By default, path is set to `/`. You can use any of the options supported by the Set-Cookie header. | ||
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie | ||
return { | ||
sessionId, | ||
}; | ||
}), | ||
) | ||
.post( | ||
"/login", | ||
defineEventHandler((event) => { | ||
setCookie(event, "sessionId", "bar", { httpOnly: true }); // By default, path is set to `/`. You can use any of the options supported by the Set-Cookie header. | ||
// @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie | ||
|
||
return 'logged in' | ||
})) | ||
return "logged in"; | ||
}), | ||
); | ||
|
||
app.use(router); | ||
|
||
app.use(router) | ||
|
||
createServer(toNodeListener(app)).listen(3000) | ||
createServer(toNodeListener(app)).listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import { createServer } from "node:http"; | ||
import { createApp, createError, createRouter, defineEventHandler, toNodeListener } from "h3" | ||
import { | ||
createApp, | ||
createError, | ||
createRouter, | ||
defineEventHandler, | ||
toNodeListener, | ||
} from "h3"; | ||
|
||
const app = createApp(); | ||
|
||
const router = createRouter() | ||
.get('/', defineEventHandler(() => { | ||
// Do not forget to return the error | ||
return createError('A simple error') // Create a 500 Internal Server Error by default | ||
})) | ||
.get('/complexe-error', defineEventHandler(() => { | ||
// You can fully customize errors by adding data, cause and if it's a fatal error or not | ||
return createError({ status: 400, message: 'Bad request', statusMessage: 'Bad request message' }) | ||
})) | ||
.get('/fatal-error', defineEventHandler(() => { | ||
// Fatal errors will stop the execution of the current request and will be logged | ||
return createError({ status: 500, message: 'Fatal error', fatal: true, data: { foo: 'bar' } }) | ||
})) | ||
.get( | ||
"/", | ||
defineEventHandler(() => { | ||
// Do not forget to return the error | ||
return createError("A simple error"); // Create a 500 Internal Server Error by default | ||
}), | ||
) | ||
.get( | ||
"/complexe-error", | ||
defineEventHandler(() => { | ||
// You can fully customize errors by adding data, cause and if it's a fatal error or not | ||
return createError({ | ||
status: 400, | ||
message: "Bad request", | ||
statusMessage: "Bad request message", | ||
}); | ||
}), | ||
) | ||
.get( | ||
"/fatal-error", | ||
defineEventHandler(() => { | ||
// Fatal errors will stop the execution of the current request and will be logged | ||
return createError({ | ||
status: 500, | ||
message: "Fatal error", | ||
fatal: true, | ||
data: { foo: "bar" }, | ||
}); | ||
}), | ||
); | ||
|
||
app.use(router) | ||
app.use(router); | ||
|
||
createServer(toNodeListener(app)).listen(3000) | ||
createServer(toNodeListener(app)).listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,53 @@ | ||
import { createServer } from "node:http"; | ||
import { createApp, defineEventHandler, toNodeListener } from "h3" | ||
import { createApp, defineEventHandler, toNodeListener } from "h3"; | ||
|
||
const app = createApp(); | ||
|
||
app | ||
// `/` is the root path and will response to every request. | ||
.use('/first-request', defineEventHandler(() => { | ||
return "hello world" | ||
})) | ||
.use('/hello', defineEventHandler(() => { | ||
return "world" | ||
})) | ||
.use('/json', defineEventHandler(() => { | ||
// Automatically set the `Content-Type` header to `application/json`. | ||
return { | ||
hello: 'world' | ||
} | ||
})) | ||
.use('/html', defineEventHandler(() => { | ||
// By default, the `Content-Type` header is set to `text/html`. | ||
return '<h1>hello world</h1>' | ||
})) | ||
.use('/buffer', defineEventHandler(() => { | ||
// No `Content-Type` header is set by default. You can set it manually using `setHeader`. | ||
return Buffer.from('hello world') | ||
})) | ||
.use('/blob', defineEventHandler(() => { | ||
// No `Content-Type` header is set by default. You can set it manually using `setHeader`. | ||
return new Blob(['hello world']) | ||
})) | ||
.use( | ||
"/first-request", | ||
defineEventHandler(() => { | ||
return "hello world"; | ||
}), | ||
) | ||
.use( | ||
"/hello", | ||
defineEventHandler(() => { | ||
return "world"; | ||
}), | ||
) | ||
.use( | ||
"/json", | ||
defineEventHandler(() => { | ||
// Automatically set the `Content-Type` header to `application/json`. | ||
return { | ||
hello: "world", | ||
}; | ||
}), | ||
) | ||
.use( | ||
"/html", | ||
defineEventHandler(() => { | ||
// By default, the `Content-Type` header is set to `text/html`. | ||
return "<h1>hello world</h1>"; | ||
}), | ||
) | ||
.use( | ||
"/buffer", | ||
defineEventHandler(() => { | ||
// No `Content-Type` header is set by default. You can set it manually using `setHeader`. | ||
return Buffer.from("hello world"); | ||
}), | ||
) | ||
.use( | ||
"/blob", | ||
defineEventHandler(() => { | ||
// No `Content-Type` header is set by default. You can set it manually using `setHeader`. | ||
return new Blob(["hello world"]); | ||
}), | ||
); | ||
|
||
// The `use` method respond to every HTTP method by default. | ||
|
||
createServer(toNodeListener(app)).listen(3000) | ||
createServer(toNodeListener(app)).listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,39 @@ | ||
import { createServer } from "node:http"; | ||
import { createApp, createRouter, defineEventHandler, getRequestHeader, getResponseHeaders, setResponseHeader, toNodeListener } from "h3" | ||
import { | ||
createApp, | ||
createRouter, | ||
defineEventHandler, | ||
getRequestHeader, | ||
getResponseHeaders, | ||
setResponseHeader, | ||
toNodeListener, | ||
} from "h3"; | ||
|
||
const app = createApp(); | ||
|
||
const router = createRouter() | ||
.get('/user-agent', defineEventHandler((event) => { | ||
const agent = getRequestHeader(event, 'user-agent') | ||
const router = createRouter().get( | ||
"/user-agent", | ||
defineEventHandler((event) => { | ||
const agent = getRequestHeader(event, "user-agent"); | ||
// You can also use `getRequestHeaders` to get all headers at once. | ||
// const headers = getRequestHeaders(event) | ||
|
||
setResponseHeader(event, 'content-type', 'text/plain') | ||
setResponseHeader(event, 'x-server', 'nitro') | ||
setResponseHeader(event, "content-type", "text/plain"); | ||
setResponseHeader(event, "x-server", "nitro"); | ||
// You can also use `setResponseHeaders` to set multiple headers at once. | ||
// setResponseHeaders(event, { 'x-server': 'nitro', 'content-type': 'text/plain' }) | ||
|
||
const responseHeaders = getResponseHeaders(event) | ||
const responseHeaders = getResponseHeaders(event); | ||
// You can also use `getResponseHeader` to get a single header. | ||
// const contentType = getResponseHeader(event, 'content-type') | ||
|
||
return { | ||
agent, | ||
responseHeaders, | ||
} | ||
})) | ||
}; | ||
}), | ||
); | ||
|
||
app.use(router); | ||
|
||
app.use(router) | ||
|
||
createServer(toNodeListener(app)).listen(3000) | ||
createServer(toNodeListener(app)).listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.