uwebsockets utilities
yarn add uwu@joshxyzhimself/uwu#v0.1.2
const uwu = require('uwu');
const uws = uwu.uws;
- Notes
handler
function hasresponse
andrequest
parameters
- Parameters
handler
-Function
, async function for handling request
response
propertiesresponse.status
- Number, for HTTP statusresponse.headers
- Object, for HTTP headersresponse.text
- String, override if sendingtext/plain
response.html
- String, override if sendingtext/html
response.json
- Object, override if sendingapplication/json
response.buffer
- Buffer, override if sendingapplication/octet-stream
response.file_name
- String, file name forcontent-disposition
response.file_dispose
- Boolean, usescontent-disposition
response.file_cache
- Boolean, cache static files in memory, defaults tofalse
response.file_cache_max_age_ms
- Number, cached static files max age in ms, defaults toInfinity
response.compress
- Boolean, compresses response, defaults tofalse
request
propertiesrequest.url
- Stringrequest.query
- Stringrequest.method
- Stringrequest.headers
- Object, HTTP headersrequest.headers.host
- Stringrequest.headers.accept
- Stringrequest.headers.accept_encoding
- Stringrequest.headers.content_type
- Stringrequest.headers.if_none_match
- Stringrequest.headers.user_agent
- Stringrequest.json
- Object, receivedapplication/json
// Logging request; serving HTML
app.get('/*', uwu.serve_handler(async (response, request) => {
console.log({ request });
response.html = `
<html>
<body>
<h4>Hello world!</h4>
</body>
</html>
`;
}));
// Setting headers; serving JSON
app.get('/test-json', uwu.serve_handler(async (response, request) => {
response.headers['Cache-Control'] = uwu.cache_control_types.no_store;
response.json = { foo: 'bar', random: Math.random() };
}));
// Serving file
app.get('/test3', uwu.serve_handler(async (response, request) => {
console.log({ request });
response.headers['Cache-Control'] = uwu.cache_control_types.no_cache;
response.file_path = __filename;
response.file_cache = true;
response.compress = true;
response.file_dispose = false;
}));
- Notes
- For serving static files
- Used before using
uwu.serve_handler
.
- Parameters
app
-Object
, uWebSockets app instanceroute_path
-String
, web route pathlocal_path
-String
, local file folder path (relative to process.cwd())response_override?
-Object
, overrides the response object, e.g. overriding cache-control headers
uwu.serve_static(app, '/scripts/', '/scripts/');
- Notes
- Exposed built-in values for
cache-control
header
- Exposed built-in values for
const cache_control_types = {
// For sensitive data
no_store: 'no-store, max-age=0',
// For dynamic data
no_cache: 'no-cache',
// For private static data (1 hour)
private_cached: 'private, max-age=3600, s-maxage=3600',
// For public static data (1 day)
public_cached: 'public, max-age=86400, s-maxage=86400',
};