New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

node-res

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-res

Nodejs http response builder

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
145K
decreased by-24.4%
Maintainers
2
Weekly downloads
 
Created
Source

Node Res

Coverage Status

node-res exposes helper methods to constructor different http response. It supports almost every method from express but is just an I/O module.

See also

  1. node-req
  2. node-cookie

Responding to requests.

const http = require('http')
const nodeRes = require('node-res')

http.createServer(function (req, res) {
  
  // plain text
  nodeRes.send(req, res, "Hello world")

  // json
  nodeRes.json(req, res, {time:"now"})

  // jsonp
  nodeRes.jsonp(req, res, {time:"now"}, "callback")

}).listen(3000)

nodeRes takes http server res object as first argument to perform any operation.

Methods

header (res, key, value)
nodeRes.header(res, 'Content-Type', 'text/html')
type (res, type, [charset=utf-8])

This method will set the content type by doing a lookup on the given type and sets charset=utf-8 by default.

nodeRes.type(res, 'html')
removeHeader (res, key)
nodeRes.removeHeader(res, 'Content-type')
getHeader (res, key)
nodeRes.getHeader(res, 'Content-type')
status (res, statusCode)
nodeRes.status(res, 200)
send(req, res, body)
nodeRes.send(req, res, {user:"someone"})
json (req, res, body)

send method is fully capable of making json responses, it is an alias method for readability.

nodeRes.json(req, res, {user:"someone"})
jsonp (req, res, body, callback="callback")
nodeRes.jsonp(req, res, {user:"someone"}, "angular")
download (req, res, filePath)
nodeRes.download(req, res, 'fullPathToFile')
attachment (req, res, filePath, name?, disposition=attachment?)

force download

nodeRes.attachment(req, res, 'fullPathToFile')
nodeRes.attachment(req, res, 'fullPathToFile', 'downloadName')
nodeRes.attachment(req, res, 'fullPathToFile', 'downloadName', 'disposition=attachment')
location (res, url)

sets location header on request

nodeRes.location(res, 'http://example.org')
redirect (req, res, url, status=302?)

redirects to given url after setting location header

nodeRes.redirect(res, 'http://example.com', 301)
vary (res, field)

Adds vary header to response, if it is not there already.

nodeRes.vary(res, 'Accept')
descriptive methods

Node res also has support for descriptive methods, they set the status itself without calling the status method.

nodeRes.ok(req, res, 'Hello world') // will set 200 as status
nodeRes.unauthorized(req, res, 'You must login first') // will set 401 as status
methodhttp response status
continue100
switchingProtocols101
ok200
created201
accepted202
nonAuthoritativeInformation203
noContent204
resetContent205
partialContent206
multipleChoices300
movedPermanently301
found302
seeOther303
notModified304
useProxy305
temporaryRedirect307
badRequest400
unauthorized401
paymentRequired402
forbidden403
notFound404
methodNotAllowed405
notAcceptable406
proxyAuthenticationRequired407
requestTimeout408
conflict409
gone410
lengthRequired411
preconditionFailed412
requestEntityTooLarge413
requestUriTooLong414
unsupportedMediaType415
requestedRangeNotSatisfiable416
expectationFailed417
unprocessableEntity422
tooManyRequests429
internalServerError500
notImplemented501
badGateway502
serviceUnavailable503
gatewayTimeout504
httpVersionNotSupported505

License

(The MIT License)

Copyright (c) 2015 Poppins

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

Keywords

FAQs

Package last updated on 08 Nov 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc