app.listen()和app.get()如何在express和hapi上工作 [英] How app.listen() and app.get() work on express and hapi

查看:242
本文介绍了app.listen()和app.get()如何在express和hapi上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用http节点模块(仅本机模块)如何重新创建app.listen()和app.get()使用带有构造函数的http模块

With http node module (only native modules) how i can recreate app.listen() and app.get() use http module with a constructor

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()


推荐答案

构建自己非常简单的HTTP fr非常容易在Node的http模块上做一些工作。这是我制作的快速实现 app.get() app.listen()方法,你可以看看它如何成长为更像Express的东西:

It's pretty easy to build your own very simple HTTP framework on top of Node's http module. Here's a quick one I made which implements the app.get() and app.listen() methods, you can see how it could grow to become something more Express-like:

'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');
};

您可以像这样使用这个迷你框架:

You can use this mini framework like so:

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请求进行测试:

You can test it with a few cURL requests:

$ 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

显然这是一个非常基本的框架,并且遇到很多像hapi这样的框架已经解决的问题:

Obviously this is a really basic framework and suffers from many problems that frameworks like hapi have solved:


  • 路径中的参数不支持,例如 /用户/ {ID} 。 URL路径必须与路径路径完全匹配

  • 添加路由的顺序很重要(这可能会导致问题)

  • 允许冲突路径

  • 缺少很多很好的功能,比如提供文件和渲染模板(尽管你可以手动在处理程序中执行此操作)

  • There's no support for parameters in paths e.g. /users/{id}. The URL paths must match the route path exactly
  • The order that you add routes is important (this can lead to issues)
  • Conflicting paths are permitted
  • Missing a lot of nice features like serving files and rendering templates (although you could do this in the handlers manually)

这篇关于app.listen()和app.get()如何在express和hapi上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆