使用Node.JS [英] Working with Node.JS

查看:113
本文介绍了使用Node.JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我转储了Windows 7,并格式化了硬盘驱动器以移植到基于Linux的操作系统,这纯粹是出于我想开始使用 Node.JS

Last night I dump windows 7 and formatted my hard driver to port to a Linux based operating system, Purely for the reasons that I wanted to start working with Node.JS

所以我已经安装了 Node.JS 并做了一些测试,包括http服务器和套接字等.

So I have installed Node.JS and have done a few test stuff, the http server and sockets etc.

我想做的是构建一个与MVC框架紧密集成的HTTP Server,但是在开始所有这些之前,我需要学习如何在Node中高效构建.

What I would like to do is build a HTTP Server that is tightly intergrated with an MVC Framework, but before I get started on all that I need to learn how to build efficiently in Node.

例如在PHP作为框架的情况下,我将创建一个引导加载系统以加载所有基类等,然后我将触发事件系统以准备开始附加回调.

For example within PHP as my framework I would create a bootloading system to load all base classes etc, then i would fire my events system ready to start attaching callbacks.

我将继续处理请求等,直到生成输出,然后将其发送到将处理标头等的输出处理程序

I would continue to process the request etc until the output is generated which then gets sent of to an output handler that would process headers etc etc

但是Node为此提供了一个全新的环境,我想知道在Node中构建系统的最佳实践.

But Node s a totally new environment for this and im wondering on the best practises to build an system in Node.

我正在寻找的信息更多地与设计结构有关,而不是与应用程序的实际编码,如何在库中加载库,在何处加载库等有关

The information im looking for is more to do with the design structure rather then the actual coding of the application, how to load the lib where to load the libs, etc etc

感谢您的帮助.

到目前为止,我的Web应用程序进展顺利,我已经很传统地构建了我的应用程序,并添加了一些程序.

So far my WebApplication is coming along nicely, I have built my application pretty traditionally and a little procedural.

我开始创建的目录结构是这样的:

What i have started out is creating a directory structure like so:

<root>
    startup.js
    /public/
        favicon.ico
        /images/
        /stylesheets/
        /javascripts/
    /system/
        init.js
        config.js
        /libs/
            /exceptions/
                http.js
                server.js
    /application/
        /views/
            /_override/
                /errors/
                    generic.view
            /partials/
                sidebar.voew
            index.view
        /controllers/
             index.js
        /models/
            users.js

此目录结构就像大多数基于MVC的Web应用程序一样,因此使用此方法感到很舒服.

This directory structure is like most MVC Based Web Applications out there so using this method I feel comfortable.

启动文件是节点作为入口点node startup &执行的文件,看起来像这样:

The startup file is whats executed by node as the entry point, node startup & and looks like so:

/*
    * Header of t he file, Copyright etc
*/

var _Intitialize = require("./system/init.js");

//Displays the command line header, title, copyright etc
_Intitialize.DisplayCommandLineHeader();

//Check the enviroment, Permissions, Ports etc
_Intitialize.CheckEnviroment();

//Start the server and listen the port.
_Initialize.StartServer();

init文件是主要工作,它告诉系统所有其他区域运行,停止等.

the init file is the main work, its what tells all other areas of the system to run, stop etc.

我在libs中有一个名为serverhandler.js,的文件,这是init.js所必需的,然后我创建一个服务器并将回调分配给ServerHandler.Listener.然后谁监听请求,检查文件是否存在于公共目录中(如果存在),然后读取大块并发回.

I have a file in libs called serverhandler.js, and this is required into init.js, I then create a server and assign the callback to the ServerHandler.Listener. Who then listens for requests, checks to see if the file exists in public directory, if so it then reads in chunks and sends back.

如果未在公共场所找到文件,则将使用Route.Create("/path?params");创建一条路由.会从uri中阻止3个元素(控制器,方法,参数),然后如果存在则加载控制器文件.

if no file was found in public it would then create a route with Route.Create("/path?params"); which deters 3 elements, Controller, Method, Params from the uri, and then the controller files are loaded if exists.

我采用了这样的方法来抛出错误页面:

I've taken on the approach of throwing error pages like so:

if(!FileSystem.exists(RequiredPath))
{
     throw new HTTPExceptions.FileNotFound();
}

希望这可以帮助某些人开始使用Node.

Hope this helps some people getting started in Node.

推荐答案

看看 http://dailyjs.com/2010/11/01/node-tutorial/,这很相关.

我也建议您查看当前的模块 https://github.com/joyent/node/wiki/modules 并阅读您感兴趣的领域中的任何项目的代码,尤其是.中间件,路由和模块加载器.

I would suggest looking at the current modules too https://github.com/joyent/node/wiki/modules and reading the code of any of the projects in the areas you are interested in, esp. the middleware, routing and module loaders.

这篇关于使用Node.JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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