表达快乐.js Restfy Koa app.listen() 和 app.get() 如何工作
How express Hapi.js Restfy Koa app.listen() and app.get() work?
使用http节点模块(仅限本机模块)如何重新创建app.listen()和app.get()使用带有构造函数的http模块
var app = function(opts) {
this.token= opts.token
}
app.prototype.get = function(callback) {
// use request and response of app.listen()
}
app.prototype.active = function(callback) {
// use request and response of app.listen()
// return on callback some manipulate
//request params
}
app.prototype.listen = function() {
// start http or https server
}
导入模块并使用它
var app = require(...)
Var client = new app({
token: 0000
})
client.get(function(error, reply) {})
client.listen()
在 Node 的 http 模块之上构建自己的非常简单的 HTTP 框架非常容易。这是我制作的一个快速方法,它实现了app.get()
和app.listen()
方法,您可以看到它如何发展成为更像 Express 的东西:
'use strict';
const Http = require('http');
const Url = require('url');
// Framework
const Framework = function (options) {
this.options = options;
this.routes = [];
this.listener = Http.createServer(this._onRequest.bind(this));
};
Framework.prototype.get = function (path, handler) {
this.routes.push({ path, method: 'GET', handler });
};
Framework.prototype.post = function (path, handler) {
this.routes.push({ path, method: 'POST', handler });
};
Framework.prototype.listen = function (callback) {
this.listener.listen(this.options.port, callback);
};
Framework.prototype._onRequest = function (req, res) {
// Find the first matching route
for (let i = 0; i < this.routes.length; ++i) {
const route = this.routes[i];
const url = Url.parse(req.url);
if (route.method === req.method && url.path === route.path) {
return route.handler(req, res);
}
}
// No matching routes
res.writeHead(404);
res.end('Not found');
};
你可以像这样使用这个迷你框架:
const app = new Framework({ port: 4000 });
app.get('/', (req, res) => {
res.end('Home page');
});
app.get('/about', (req, res) => {
res.end('About page');
});
app.listen(() => {
console.log('Started server!');
});
您可以使用一些 cURL 请求对其进行测试:
$ curl -i http://localhost:4000/
HTTP/1.1 200 OK
Date: Sun, 24 Apr 2016 14:38:02 GMT
Connection: keep-alive
Content-Length: 9
Home page
$ curl -i http://localhost:4000/about
HTTP/1.1 200 OK
Date: Sun, 24 Apr 2016 14:38:08 GMT
Connection: keep-alive
Content-Length: 10
About page
$ curl -i http://localhost:4000/spaghetti
HTTP/1.1 404 Not Found
Date: Sun, 24 Apr 2016 14:38:14 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Not found
显然,这是一个非常基本的框架,并且遇到了许多像happy这样的框架已经解决的问题:
- 路径中不支持参数,例如
/users/{id}
.URL 路径必须与路由路径完全匹配 - 添加路由的顺序很重要(这可能会导致问题)
- 允许冲突路径
- 缺少许多不错的功能,例如提供文件和渲染模板(尽管您可以在处理程序中手动执行此操作)
相关文章:
- 等待 MongoDB findOne 回调完成,然后再完成 app.get()
- 表达快乐.js Restfy Koa app.listen() 和 app.get() 如何工作
- 如何避免为我的每个html文件添加ExpressJS app.get()
- 如何在express和parse中使用app.get中的app.locals
- 什么's将数组传递给app.get()的第一个参数的替换项
- 在连接DB - NodeJs的函数中使用app.get
- 如何将所选复选框的值发送到 app.get 路由
- NodeJS : app.get() 不像我所说的那样运行
- 为什么我不能从节点和 express.js 中的 app.get 函数中提取回调
- app.get and app.post get value in node.js
- Express app.get() equivalent in CasperJS
- Node.js Express app.get:浏览器想要打开链接
- Express Middleware为所有app.get()填充Jade变量
- 为什么总是触发“app.get('*')”
- 带有查询参数的节点js-app.get方法-内部服务器错误
- app.get在Node/Express中返回undefined
- app.get 不适用于基本路由
- Node.js Express app.get()没有'不在cron中工作
- 使用app.Get(节点)获取并渲染一些文件
- express.js:如何在app.get中使用http.request返回的值