Example #1
0
        request.on('end', function () {
            var parsedURL = url.parse(request.url);

            session(request, response, function (request, response) {
                if (parsedURL.pathname.search(/backend/) > -1) {
                    var router = require('./router').createRouter(model, authentication);
                    var emitter = router.handle(request, body, function (route) {
                        var headers = route.headers;
                        // aleays inject CORS header
                        headers[corsHeaderName] = 'http://' + request.headers.host;

                        // send response
                        response.writeHead(route.status, headers);
                        response.end(route.body);
                    });
                } else {
                    if (request.session.data.user === 'Guest' && !isIndexRequest(parsedURL.pathname) ) {
                        response.writeHead(302, {
                            'Location': '/'
                        });
                        response.end();
                    } else {
                        send(request, parsedURL.pathname)
                            .root(path.join(__dirname, '..', '..', 'app'))
                            .pipe(response);
                    }
                }
            });
        });
Example #2
0
module.exports.prototype.main = function(req, resp) {
    try {
        if (this._allowedMethods.indexOf(req.method) == -1) {
            this.responseManager.writeMethodNotAllowed(resp, req.method);
            return;
        }

        var _this = this;

        sesh.session(req, resp, function(req, resp) {
            try {
                var filename = req.url.split('?')[0];

                // req.session was created by sesh
                _this.requestManager.session = req.session;

                // fetch the body
                var body = null;
                var rawBody = '';

                // TODO: upgrade to streams2

                req.on('data', function(data) {
                    if (rawBody.length > _this.reqBodyMaxLength) {
                        req.connection.destroy();
                    }

                    rawBody += data;
                });

                req.on('end', function() {
                    var requestData = new request.Data(req, rawBody);

                    _this.requestManager._requestData = requestData;
                    _this.requestManager.run(resp, filename);
                });

            } catch (e) {
                _this.responseManager.writeInternalServerError(resp, e);
                _this.emit('error', e);
            }
        });
    } catch(e) {
        this.responseManager.writeInternalServerError(resp, e);
        this.emit('error', e);
    }
};