Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mvcfun

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mvcfun - npm Package Compare versions

Comparing version 0.1.5 to 0.2.0

lib/request/Data.js

46

lib/controller/Base.js

@@ -46,5 +46,5 @@ /**

* The current language of the controller. Has to be one of the iso strings
* 'en', 'de', ... This will be added to any reponse header at run:
* 'en', 'de', ... This will be added to any response header at run:
*
* For instanct: 'Content-language: en'.
* For instance: 'Content-language: en'.
*

@@ -170,13 +170,10 @@ * If not set, the header line will not be added.

/**
* The parsed request body aka post data.
* The data of the current request.
*
* If this is null, the body could not be parsed. You may want to check then the
* raw body instead.
*
* @member {Object|null} body
* @member {request.Data}
* @readonly
*/
nepp.createGS(module.exports.prototype, 'body',
function getBody() {
return this.requestManager.body;
nepp.createGS(module.exports.prototype, 'request',
function getRequest() {
return this.requestManager.requestData;
}

@@ -186,24 +183,2 @@ );

/**
* The raw request body aka raw post data.
*
* @member {String} rawBody
*/
nepp.createGS(module.exports.prototype, 'rawBody',
function getRawBody() {
return this.requestManager.rawBody;
}
);
/**
* The parsed query string aka get data.
*
* @member {Object} query
*/
nepp.createGS(module.exports.prototype, 'query',
function getQuery() {
return this.requestManager.query;
}
);
/**
* Call this, if you want to log something in your controller.

@@ -234,6 +209,9 @@ *

method + "\n" +
'request body:' + JSON.stringify(this.body) + "\n" +
'query:' + JSON.stringify(this.query),
'headers:' + JSON.stringify(this.request.headers) + "\n" +
'host:' + this.request.host + "\n" +
'port:' + this.request.port + "\n" +
'request body:' + JSON.stringify(this.request.body) + "\n" +
'query:' + JSON.stringify(this.request.query),
this.language
);
};

@@ -9,3 +9,2 @@ /**

util = require('util'),
url = require('url'),
fs = require('fs'),

@@ -252,10 +251,7 @@ path = require('path'),

req.on('end', function() {
try {
var body = _this._parse_body(rawBody);
} catch (e) {
var body = null;
}
var query = url.parse(req.url, true).query || {};
var requestData = new request.Data(req, rawBody);
_this.requestManager.setQueryAndBody(query, body, rawBody);
// REDIRECT HERE
_this.requestManager._requestData = requestData;
_this.requestManager.run(filename, req.method, resp);

@@ -301,14 +297,2 @@ });

/**
* Parses the body.
*
* @param {String} rawBody The raw body to parse.
* @returns {Object}
* @throws {Error} if it could not be parsed at all.
* @protected
*/
module.exports.prototype._parse_body = function(rawBody) {
return JSON.parse(VC.string(rawBody, 'rawBody'));
};
/**
* This method logs a message as specified.

@@ -315,0 +299,0 @@ *

@@ -9,3 +9,4 @@ /**

Controller: require('./Controller'),
Manager: require('./Manager')
Manager: require('./Manager'),
Data: require('./Data')
};

@@ -7,3 +7,3 @@ /**

*
* @module mvcfun.request.manager
* @module mvcfun.request.Manager
* @author Philipp Kemmeter

@@ -15,2 +15,3 @@ */

util = require('util'),
url = require('url'),
nepp = require('nepp');

@@ -56,23 +57,10 @@

/**
* Contains the parsed body of the current request.
* Contains the request data. Will be set on every request by the
* http.Server instance.
*
* @var {request.Data}
* @protected
*/
this._body = null;
this._requestData = null;
/**
* Contains the raw body of the current request.
*
* @protected
*/
this._rawBody = '';
/**
* Contains the query as object of the current request; i.e. the parsed
* query string.
*
* @protected
*/
this._query = null
nepp(this);

@@ -92,2 +80,12 @@

/**
* Caught errors are emitted via this method.
*
* @param {Object} err Error occured.
* @protected
*/
module.exports.prototype._error = function(err) {
this.emit('error', err);
};
/**
* Handles request to files to be delivered.

@@ -120,2 +118,4 @@ *

// {{{ Getter / Setter
/**

@@ -126,72 +126,40 @@ * The controller manager helper object.

*/
Object.defineProperty(module.exports.prototype, 'requestController', {
get: function getRequestController() {
nepp.createGS(module.exports.prototype, 'requestController',
function getRequestController() {
return this._requestController;
},
set: function setRequestController(requestController) {
function setRequestController(requestController) {
this._requestController = VC.instance_of(
requestController, 'requestController', require('./Controller')
);
},
enumerable: true
});
}
);
/**
* The query as object of the current request, i.e. the parsed query string.
* The session object. Changable on the fly.
*
* @member {Object} query
* @readonly
* @member {Object} session
*/
Object.defineProperty(module.exports.prototype, 'query', {
get: function getQuery() {
return this._query;
nepp.createGS(module.exports.prototype, 'session',
function getSession() {
return this._session;
},
enumerable: true
});
function setSession(session) {
this._session = VC.instance_of(session, 'session', Object);
}
);
/**
* The parsed body object.
* Contains the data of the current request like headers etc.
*
* If this returns null, the body could not be parsed. Check the raw body in
* this case.
*
* @member {Object|null} body
* @member {request.Data}
* @readonly
*/
Object.defineProperty(module.exports.prototype, 'body', {
get: function getBody() {
return this._body;
},
enumerable: true
});
nepp.createGS(module.exports.prototype, 'requestData',
function getSession() {
return this._requestData;
}
);
/**
* The raw body. I.e. this is an unparsed plain raw string.
*
* @member {String} rawBody
* @readonly
*/
Object.defineProperty(module.exports.prototype, 'rawBody', {
get: function getRawBody() {
return this._rawBody;
},
enumerable: true
});
/**
* The session object. Changable on the fly.
*
* @member {Object} session
*/
Object.defineProperty(module.exports.prototype, 'session', {
get: function getSession() {
return this._session;
},
set: function setSession(session) {
this._session = VC.instance_of(session, 'session', Object);
},
enumerable: true
});
/**
* The response manager.

@@ -201,40 +169,15 @@ *

*/
Object.defineProperty(module.exports.prototype, 'responseManager', {
get: function getResponseManager() {
nepp.createGS(module.exports.prototype, 'responseManager',
function getResponseManager() {
return this._responseManager;
},
set: function setResponseManager(respManager) {
function setResponseManager(respManager) {
this._responseManager = VC.instance_of(
respManager, 'respManager', require('../response/Manager')
);
},
enumerable: true
});
}
);
/**
* Sets the query object, the body object and the raw body string.
*
* This is a combi setter, because those values must not be changed but by the
* Server object owning this manager.
*
* @param {Object} query Query object.
* @param {Object|null} body Body object.
* @param {String} rawBody Raw body.
*/
module.exports.prototype.setQueryAndBody = function(query, body, rawBody) {
this._query = VC.instance_of(query, 'query', Object);
this._body = (body === null) ? null : VC.instance_of(body, 'body', Object);
this._rawBody = VC.string(rawBody, 'rawBody', true);
};
// }}}
/**
* Caught errors are emitted via this method.
*
* @param {Object} err Error occured.
* @protected
*/
module.exports.prototype._error = function(err) {
this.emit('error', err);
};
nepp(module.exports.prototype);
{
"name": "mvcfun",
"version": "0.1.5",
"version": "0.2.0",
"description": "MVC based web server framework",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -39,2 +39,3 @@ # MVCfun

- MVC based developing the cool way
- Easy controller handling
- Maintainable code

@@ -47,2 +48,38 @@ - Increadibly fast

## Custom Controller
The main idea of MVCFun is to design your web project based on model, views and
controllers. The key part are the controllers, because they connect the models
with the views.
Therefore it's absolutely necessary to be able to create custom controllers very
easily. This is one main feature of MVCFun.
// The Hello World
var mvcfun = require('mvcfun'),
controller = mvcfun.controller,
util = require('util'),
regexp = mvcfun.regexp;
var HelloWorldController = function(filename) {
controller.Base.call(this, filename);
};
util.inherits(HelloWorldController, controller.Base);
HelloWorldController.prototype.run = function(method, resp, path) {
this.responseManager.writeText(resp,'Hello World!');
};
var server = (new mvcfun.http.Server()).listen(8080);
server.addController(new HelloWorldController('/helloworld'));
If you run this example and surfs at "localhost:8080/helloworld" you will read
"Hello World!" as plain text response.
For sure, the controllers should be organized in seperate files (one per
controller). The controller can easily require any class to connect to them
(models) and use any kind of template engine to create the view. There is build
in swig support.
## Installation

@@ -49,0 +86,0 @@

@@ -19,2 +19,5 @@ /**

var reqCtrl = reqMan.requestController;
reqMan._requestData = new mvcfun.request.Data(
new http.IncomingMessage(), ''
);
//

@@ -21,0 +24,0 @@ // Tests the run method

@@ -8,2 +8,3 @@ /*

util = require('util'),
http = require('http'),
should = require('should');

@@ -50,2 +51,66 @@

});
describe('#main', function() {
var allowed = [];
for (var i = 0; i < mvcfun.http.Server.METHODS.length; ++i) {
allowed[mvcfun.http.Server.METHODS[i]] = false;
}
allowed['GET'] = allowed['POST'] = true;
for (var m in allowed) {
it(
'should respect allowedMethods ['
+ m + ': ' + allowed[m] + ']',
(function(method, is_allowed) {
return function(done) {
var MyRespMan = function() {};
util.inherits(MyRespMan, mvcfun.response.Manager);
var myRespMan = new MyRespMan();
myRespMan.writeMethodNotAllowed
= function(_resp, _method)
{
if (is_allowed) {
done('Allowed method, but 405');
return;
} else {
_resp.should.equal(resp);
_method.should.equal(method);
done();
}
};
myRespMan.writeInternalServerError
= function(_resp, e)
{
done(e);
};
var server = new mvcfun.http.Server({
allowedMethods: ['GET', 'POST'],
responseManager: myRespMan
});
var req = new http.IncomingMessage();
req.method = method;
req.on = function(ev) {
if (ev != 'end')
return;
if (!is_allowed)
done('Not allowed method, but req man called');
else
done();
};
var resp = function() {
this.setHeader = function(){};
};
util.inherits(resp, http.ServerResponse);
resp.setHeader = function(){};
server.main(req, resp);
};
})(m, allowed[m])
);
}
});
});

@@ -15,27 +15,2 @@ /**

describe('#setQueryAndBody', function() {
var reqMan = new mvcfun.request.Manager(
respMan,
new mvcfun.request.Controller()
);
it('should set properly', function() {
var query = {lala: 'ulu'};
var body = {cool: 'object'};
var rawBody = 'cool object';
reqMan.setQueryAndBody(query, body, rawBody);
reqMan.query.should.equal(query);
reqMan.body.should.equal(body);
reqMan.rawBody.should.equal(rawBody);
});
it('should work properly with empty arguments', function() {
var query = {};
var body = {};
var rawBody = '';
reqMan.setQueryAndBody(query, body, rawBody);
reqMan.query.should.equal(query);
reqMan.body.should.equal(body);
reqMan.rawBody.should.equal(rawBody);
});
});
//

@@ -42,0 +17,0 @@ // Tests request.Manager.run

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc