Za
Za is a simple web server that exposes an Express-like API and is capable of handling far more requests per second than Express.
Quick Start
Install za
in your project:
npm install --save za
Use za
to start an HTTP server:
var server = require('./za')();
server.listen(8888);
server.get('/', function (request, response) {
response.send('Hello World!');
});
console.log('Visit http://localhost:8888/ for "Hello World!"');
Server and Router
za()
The za
API is a function which instantiates servers.
var za = require('za');
var server1 = za({port: 8001});
var server2 = za({port: 8002});
console.log('HTTP servers are listening on 8001 and 8002');
server.router
Za exposes its Router
object via server.router
.
router.use([path,] fn)
The use
method on a router sets a middleware function, optionally to be
routed only to a given path. The handler function takes request
, response
and next
arguments. The request
and response
arguments are HTTP request
and response objects, and the next
argument is a function that you should
invoke when you want the next middleware to start executing. For performance
reasons, wildcard paths are not yet supported, but the middleware executes for
any request URLs that start with that path.
This can be used for things like logging:
server.use(function (request, response, next) {
console.log('User requested "' + request.url + '"');
next();
});
Or for something like authentication:
server.use('/admin', function (request, response, next) {
if (request.isAdminUser) {
next();
} else {
response.statusCode = 401;
response.end("You are not signed in as an admin user.");
}
});
router.get(path, fn)
The get
method on a router sets a GET handler function for a given
path. The handler function takes request
and response
arguments. For
performance reasons, wildcard paths are not yet supported.
server.get('/ping', function (request, response) {
response.end('OK');
});
router.post(path, fn)
Works just like router.get, only the method is POST.
router.put(path, fn)
Works just like router.get, only the method is PUT.
router.delete(path, fn)
Works just like router.get, only the method is DELETE.
server.use(path, fn)
Calls router.use
on the server's router.
server.get(path, fn)
Calls router.get
on the server's router.
server.post(path, fn)
Calls router.post
on the server's router.
server.put(path, fn)
Calls router.put
on the server's router.
server.delete(path, fn)
Calls router.delete
on the server's router.
HTTP object decorations
Za decorates the http.IncomingMessage
and http.ServerResponse
prototypes
with useful methods.
request.header(name)
Returns the value of a response header with the given name.
request.cookies
Exposes a getter which parses the cookie header and returns an object with a key and value for each cookie.
request.body
Exposes a getter which parses the request body and returns an object with a key and value for each parameter in the POST body.
request.query
Exposes a getter which parses the query string and returns an object with a key and value for each URL parameter.
request.multipart
The multipart
object has its keys and values set as the server is receiving
multipart form data. If the request is not a multipart type request, this
object will be undefined (so it can be used to test for multipart requests).
response.json(object)
Sends an object as a JSON string, with content-type: application/json
.
response.send(data)
Sends a string, an HTTP status, or an object as a JSON string - depending on
the type of the data (string
, number
or object
).
response.zip(text, preZipped)
Uses gzip
to compress text (provided the accept-encoding
header mentions
"gzip"), then sets content-encoding: gzip
and sends the response.
NOTE: If preZipped
is passed in, the gzip
compression step is skipped, and
the pre-zipped content is used instead. This is a useful performance feature
for when you need to send the same zipped response multiple times.
response.cookie(name, value, options)
Sets a cookie with a given name, value and options.
response.redirect(location)
Sends a 302 response, redirecting to the given location.
Multipart
Za includes its own multipart parser. When a multipart request is received, its handler is called before the multipart parser begins. The handler can listen for request events as file parsing proceeds.
request.on("za:file", fn)
When the multipart parser finds a file, the request emits a "za:file"
event
and passes the file field. A listener can set a stream
object on the file
field, and the parser will save to that stream. When the parser is finished, it
will close the stream.
server.post('/upload', function (request, response) {
request.on('za:file', function (field) {
var path = '/tmp/upload';
var encoding = (/text/.test(field.type) ? 'utf8' : 'binary');
field.stream = fs.createWriteStream(, encoding);
field.stream.on('close', function () {
response.send('File "' + field.filename + '" saved to "' + path + '".');
});
});
});
request.on("za:finished", fn)
When the multipart parser is finished, the request emits a "za:finished"
event. At that time, request.multipart
(as well as request.body
) contain
all of the data that was sent in the multipart request.
Why is it called "Za"?
The term "za" is short for "pizza", and when it comes to web app responses and pizza, everyone wants fast delivery. (Also, the name was available on NPM).
Acknowledgements
We would like to thank all of the amazing people who use, support, promote, enhance, document, patch, and submit comments & issues. Za couldn't exist without you.
Additionally, huge thanks go to Goin’ for employing and supporting Za project maintainers, and for being an epically awesome place to work (and play).
MIT License
Copyright (c) 2014 Sam Eubank
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 SOFTWARE.
How to Contribute
We welcome contributions from the community and are happy to have them. Please follow this guide when logging issues or making code changes.
Logging Issues
All issues should be created using the new issue form. Please describe the issue including steps to reproduce. Also, make sure to indicate the version that has the issue.
Changing Code
Code changes are welcome and encouraged! Please follow our process:
- Fork the repository on GitHub.
- Fix the issue ensuring that your code follows the style guide.
- Add tests for your new code, ensuring that you have 100% code coverage.
(If necessary, we can help you reach 100% prior to merging.)
- Run
npm test
to run tests quickly, without testing coverage. - Run
npm run cover
to test coverage and generate a report. - Run
npm run report
to open the coverage report you generated.
- Run
- Pull requests should be made to the master branch.
Contributor Code of Conduct
As contributors and maintainers of Za, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
If any participant in this project has issues or takes exception with a contribution, they are obligated to provide constructive feedback and never resort to personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, edits, issues, and other contributions that are not aligned with this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
We promise to extend courtesy and respect to everyone involved in this project regardless of gender, gender identity, sexual orientation, ability or disability, ethnicity, religion, age, location, native language, or level of experience.