Thursday, January 16, 2014

Simple usage Router in Node JS

what you need is connect library and connect_router lib

if you want to install command npm install connect



/**
 * Created by David on 14. 1. 17.
 */
var connect = require('connect');

var router = require('connect_router');

var server = connect.createServer();

server.use(connect.logger());
server.use(connect.query());
server.use(connect.static(__dirname + '/images'));
server.use(router(function (app) {
    app.get('/home/index', function (request, response, next) {
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write('index');
        response.end();
    });

    app.get('*', function (request, response, next) {
        response.writeHead(200, {'Content-Type': 'text/html'});
        response.write('error');
        response.end();
    });
}));

server.listen(8888, function () {
   console.log('8888 is running');
});

No comments:

Post a Comment