root

2019-09-14 admin

root是什么

什么是root,a super lightweight web framework featuring prototype mixin support and routing

root使用教程帮助文档

root

A super lightweight web framework with routing and prototype mixin support.

It’s available through npm:

npm install root

build status

Usage

Usage is simple

var root = require('root');
var app = root();

app.get('/', function(request, response) {
	response.send({hello:'world'});
});

app.post('/echo', function(request, response) {
	request.on('json', function(body) {
		response.send(body);
	});
});

app.listen(8080);

You can extend the request and response with your own methods

app.use('response.time', function() {
	this.send({time:this.request.time});
});
app.use('request.time', {getter:true}, function() {
	return Date.now();
});

app.get(function(request, response) {
	response.time();
});

Routing

Routing is done using murl. Use the get, post, put, del, patch or options method to specify the HTTP method you want to route

app.get('/hello/{world}', function(request, response) {
	response.send({world:request.params.world});
});
app.get('/test', function(request, response, next) {
	// call next to call the next matching route
	next();
});
app.get('/test', function(request, response) {
	response.send('ok');
});

URL normalization

Before routing an incoming url it is first decoded and normalized

  • /../..//
  • /foo/bar/../baz/foo/baz
  • /foo%20bar/foo bar
  • /foo%2fbar/foo/bar

This basicly means that you don’t need to worry about /.. attacks when serving files or similar.

Error handling

You can specify an error handler for a specific error code by using the error function

app.get('/foo', function(request, response) {
	response.error(400, 'bad request man');
});

app.error(404, function(request, response, opts) {
	// opts contains .message which is the message passed to response.error
	// and .stack if an error was passed
	response.send({error:'could not find route'});
});
app.error(function(request, response, opts) {
	response.send({error:'catch all other errors'});
});

Using sub apps

Route requests through an sub app by using app.route

var mobileApp = root();
var myApp = root();
...
myApp.all('/m/*', function(request, response, next) {
	// all routes starting with /m should route through our mobile app as well
	mobileApp.route(request, response, next);
});

As a shortcut you can just pass the app directly

myApp.all('/m/*', mobileApp);

This allows you to easily split up your application into seperate parts and mount them all on one server

Full API

Response

  • response.send(json) will send back json.
  • response.send(string) will send back html (if no Content-Type has been set).
  • response.error(statusCode, messageOrError) send back an error
  • response.redirect(url) send a http redirect

Request

  • request.on('json', listener) will buffer and parse the body as JSON.
  • request.on('form', listener) will buffer and parse the body as a url encoded form
  • request.on('body', listener) will buffer the body as a string
  • request.query contains the parsed querystring from the url

App

  • app.use(methodName, options, fn) extend the request or response with a new prototype method
  • app.(get|put|post|del|options|patch)(pattern, fn) add a route for a http method
  • app.all(pattern, fn) route all methods
  • app.route(request, response, callback) route a request or response from another app
  • app.error(statusCode, fn) add an error handler. use 4xx to match all 400 errors etc.
  • app.on('route', function (request, response) {}) emitted every time a request is being routed
  • app.on('match', function (request, response, pattern) {}) emitted every time a URL pattern is matched

License

MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-75077.html

文章标题:root

相关文章
root-path
root-path是什么 什么是root-path,Normalize paths, joined with project's root path root-path官网:官网 root-path源码仓库:源码仓库 root-path下...
2019-02-19
CentOS环境中MySQL修改root密码方法
环境相关: OS:CentOS release 6.9 IP:192.168.1.10 MySQL:MariaDB-10.1.30 1. 修改前的确认 修改root密码是需要重启mysql库,确认生产真的可以重启mysql库; 确认生产是否...
2018-01-08
为什么我们需要添加“root”
Tinggo提出了一个问题:Why we need to add <div id=“fb-root”></div>,或许与您遇到的问题类似。 回答者Simon Hutton给出了该问题的处理方式: It’s the ...
2018-03-27
degroote-jquery-datetimepicker
degroote-jquery-datetimepicker是什么 什么是degroote-jquery-datetimepicker,jQuery Plugin DateTimePicker it is DatePicker and Ti...
2019-03-10
path-root-regex
path-root-regex是什么 什么是path-root-regex,Regular expression for getting the root of a posix or windows filepath. path-root...
2018-12-17
root-require
root-require是什么 什么是root-require,For use in tests only! require() using a relative path from the root directory of the pr...
2019-02-18
rootpath
rootpath是什么 什么是rootpath,Little helper to make node.js require relative to your project root rootpath官网:官网 rootpath源码仓库:...
2019-05-15
postcss-atroot
postcss-atroot是什么 什么是postcss-atroot,PostCSS plugin to place rules directly at the root postcss-atroot官网:官网 postcss-atro...
2018-10-22
ReactRoot与ReactWork源码分析
在ReactDOM.render源码解析-1中介绍了第一次render的基本过程的一部分,其中产生了ReactRoot和ReactWork两个类的实例。本文介绍下ReactRoot,ReactWork源码,只关注第一次调用render的过...
2019-04-21
find-root-package
find-root-package是什么 什么是find-root-package,Find the furthest package.json find-root-package官网:官网 find-root-package源码仓库:源...
2019-02-11
回到顶部