How to use statuses - 10 common examples

To help you get started, we’ve selected a few statuses examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github andreirtaylor / BoardGameTracking / node_modules / nodemon / node_modules / update-notifier / node_modules / latest-version / node_modules / package-json / node_modules / got / index.js View on Github external
var req = fn.request(arg, function (response) {
			var statusCode = response.statusCode;
			var res = response;

			// redirect
			if (status.redirect[statusCode] && 'location' in res.headers) {
				res.resume(); // Discard response

				if (++redirectCount > 10) {
					cb(new Error('Redirected 10 times. Aborting.'), undefined, res);
					return;
				}

				get(urlLib.resolve(url, res.headers.location), opts, cb);
				return;
			}

			if (['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
				var unzip = zlib.createUnzip();
				res.pipe(unzip);
				res = unzip;
			}
github metrue / fx / images / node / node_modules / koa / lib / response.js View on Github external
set body(val) {
    const original = this._body;
    this._body = val;

    if (this.res.headersSent) return;

    // no content
    if (null == val) {
      if (!statuses.empty[this.status]) this.status = 204;
      this.remove('Content-Type');
      this.remove('Content-Length');
      this.remove('Transfer-Encoding');
      return;
    }

    // set the status
    if (!this._explicitStatus) this.status = 200;

    // set the content-type only if not yet set
    const setType = !this.header['content-type'];

    // string
    if ('string' == typeof val) {
      if (setType) this.type = /^\s*
github qiushi123 / xiaochengxu_demos / 014云开发实现小程序支付 / cloud / pay / node_modules / urllib / lib / urllib.js View on Github external
function handleRedirect(res) {
        var err = null;
        if (args.followRedirect && statuses.redirect[res.statusCode]) {  // handle redirect
            args._followRedirectCount = (args._followRedirectCount || 0) + 1;
            var location = res.headers.location;
            if (!location) {
                err = new Error('Got statusCode ' + res.statusCode + ' but cannot resolve next location from headers');
                err.name = 'FollowRedirectError';
            } else if (args._followRedirectCount > args.maxRedirects) {
                err = new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + url);
                err.name = 'MaxRedirectError';
            } else {
                var newUrl = args.formatRedirectUrl ? args.formatRedirectUrl(url, location) : urlutil.resolve(url, location);
                debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
                // make sure timer stop
                cancelResponseTimer();
                // should clean up headers.Host on `location: http://other-domain/url`
                if (options.headers.Host && PROTO_RE.test(location)) {
                    options.headers.Host = null;
github node-modules / urllib / lib / urllib.js View on Github external
function handleRedirect(res) {
    var err = null;
    if (args.followRedirect && statuses.redirect[res.statusCode]) {  // handle redirect
      args._followRedirectCount = (args._followRedirectCount || 0) + 1;
      var location = res.headers.location;
      if (!location) {
        err = new Error('Got statusCode ' + res.statusCode + ' but cannot resolve next location from headers');
        err.name = 'FollowRedirectError';
      } else if (args._followRedirectCount > args.maxRedirects) {
        err = new Error('Exceeded maxRedirects. Probably stuck in a redirect loop ' + url);
        err.name = 'MaxRedirectError';
      } else {
        var newUrl = args.formatRedirectUrl ? args.formatRedirectUrl(url, location) : urlutil.resolve(url, location);
        debug('Request#%d %s: `redirected` from %s to %s', reqId, options.path, url, newUrl);
        // make sure timer stop
        cancelResponseTimer();
        // should clean up headers.Host on `location: http://other-domain/url`
        if (options.headers.Host && PROTO_RE.test(location)) {
          options.headers.Host = null;
github koajs / compress / index.js View on Github external
return async (ctx, next) => {
    ctx.vary('Accept-Encoding')

    await next()

    let { body } = ctx
    if (!body) return
    if (ctx.res.headersSent || !ctx.writable) return
    if (ctx.compress === false) return
    if (ctx.request.method === 'HEAD') return
    if (status.empty[ctx.response.status]) return
    if (ctx.response.get('Content-Encoding')) return

    // forced compression or implied
    if (!(ctx.compress === true || filter(ctx.response.type))) return

    // identity
    const encoding = ctx.acceptsEncodings('gzip', 'deflate', 'identity')
    if (!encoding) ctx.throw(406, 'supported encodings: gzip, deflate, identity')
    if (encoding === 'identity') return

    // json
    if (isJSON(body)) body = ctx.body = JSON.stringify(body)

    // threshold
    if (threshold && ctx.response.length < threshold) return
github polixjs / polix / extends / application.js View on Github external
function respond(ctx) {
  // allow bypassing koa
  if (false === ctx.respond) return;

  const res = ctx.res;
  if (!ctx.writable) return;

  let body = ctx.body;
  const code = ctx.status;

  // ignore body
  if (statuses.empty[code]) {
    // strip headers
    ctx.body = null;
    return res.end();
  }

  if ('HEAD' == ctx.method) {
    if (!res.headersSent && isJSON(body)) {
      ctx.length = Buffer.byteLength(JSON.stringify(body));
    }
    return res.end();
  }

  // status body
  if (null == body) {
    body = ctx.message || String(code);
    if (!res.headersSent) {
github andywer / srv / src / core / response.ts View on Github external
const headers = response.headers || {}

  res.statusCode = response.status
  res.statusMessage = statuses[response.status] || res.statusMessage

  // Using for..in here, since it's faster than Object.keys()
  for (const headerName in headers) {
    const headerValue = headers[headerName]
    if (typeof headerValue !== "undefined") {
      res.setHeader(headerName, headerValue)
    }
  }

  if (body && statuses.empty[response.status]) {
    debug(Error(`Warning: Response body defined on a ${response.status} response.`))
  } else if (!body && !statuses.empty[response.status]) {
    debug(Error(`Warning: No response body defined on a ${response.status} response.`))
  }

  if (Buffer.isBuffer(body)) {
    return res.end(body)
  } else if (isStream(body)) {
    return body.pipe(res)
  } else if (body) {
    throw Error(`Unexpected Response.body: ${body}`)
  } else {
    return res.end()
  }
}
github andywer / srv / src / core / response.ts View on Github external
export function applyResponseTo(response: Response, res: http.ServerResponse) {
  const body = response.body
  const headers = response.headers || {}

  res.statusCode = response.status
  res.statusMessage = statuses[response.status] || res.statusMessage

  // Using for..in here, since it's faster than Object.keys()
  for (const headerName in headers) {
    const headerValue = headers[headerName]
    if (typeof headerValue !== "undefined") {
      res.setHeader(headerName, headerValue)
    }
  }

  if (body && statuses.empty[response.status]) {
    debug(Error(`Warning: Response body defined on a ${response.status} response.`))
  } else if (!body && !statuses.empty[response.status]) {
    debug(Error(`Warning: No response body defined on a ${response.status} response.`))
  }

  if (Buffer.isBuffer(body)) {
    return res.end(body)
  } else if (isStream(body)) {
    return body.pipe(res)
  } else if (body) {
    throw Error(`Unexpected Response.body: ${body}`)
  } else {
    return res.end()
  }
}
github rill-js / rill / src / respond.js View on Github external
// Skip request ended externally.
  if (original.headersSent) return

  // Apply default statuses.
  if (Number(res.status) === 404) {
    // Ensure redirect status.
    if (res.get('Location')) res.status = 302
    // Default the status to 200 if there is substance to the response.
    else if (body) res.status = 200
  }

  // Default status message based on status code.
  res.message = res.message || statuses[res.status]

  // Ensure no content-type for empty responses.
  if (req.method === 'HEAD' || statuses.empty[res.status] || !body) {
    body = null
    res.remove('Content-Type')
    res.remove('Content-Length')
  } else {
    // Attempt to guess content type.
    if (!res.get('Content-Type')) res.set('Content-Type', checkType(body))
    // Stringify objects that are not buffers.
    if (typeof body === 'object' && !isStream && !isBuffer) body = JSON.stringify(body)
    // Attempt to guess content-length.
    if (!res.get('Content-Length') && !isStream) res.set('Content-Length', byteLength(body))
  }

  // Send off headers.
  original.writeHead(res.status, res.message, removeEmptyHeaders(res.headers))
  // Allow for requests to stay open.
  if (res.end === false) return
github eggjs / egg-security / lib / middlewares / nosniff.js View on Github external
return async function nosniff(ctx, next) {
    await next();

    // ignore redirect response
    if (statuses.redirect[ctx.status]) return;

    const opts = utils.merge(options, ctx.securityOptions.nosniff);
    if (utils.checkIfIgnore(opts, ctx)) return;

    ctx.set('x-content-type-options', 'nosniff');
  };
};

statuses

HTTP status utility

MIT
Latest version published 4 years ago

Package Health Score

71 / 100
Full package analysis