开发者

Working with 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

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

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.

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

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开发者_JAVA百科 coding of the application, how to load the lib where to load the libs, etc etc

Any help is appreciated.


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

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

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

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

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.

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

Hope this helps some people getting started in Node.


Have a look at http://dailyjs.com/2010/11/01/node-tutorial/ , it's pretty relevant.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜